开发者

PHP embed multiple mp3 tracks after uploading using echo and <audio>

I have my html5 form working to upload multiple mp3 files with one submit button but I want them all to immediately appear (embed) on the page with the tag. Currently I can only get one.

Q: How do I set echo for the other id's (audio-player2, audioplayer3 etc) to appear?

开发者_运维技巧I have tried adding a second echo with the same stuff and substituting audio-player2 but it (understandably) plays the same file. presumably because of (name="files[]")

here's the html:

<form method="post" enctype="multipart/form-data">
            <input type="file" name="files[]" multiple="multiple">
            <input type="submit" value="upload!">
            </form>
here's the php:
<?php
if (isset($_FILES['files'])){
    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name){
        move_uploaded_file($tmp_name, "uploaded/{$_FILES['files']['name'][$key]}");
    }
echo "<br><audio preload controls src='uploaded/{$_FILES['files']['name'][$key]}' id='audio-player1' name='audio-player1'></audio><br>";
}
?>


Move the echo statement inside the foreach loop and add a counting variable:

<?php
if (isset($_FILES['files'])){
    $i = 0;
    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name){
        move_uploaded_file($tmp_name, "uploaded/{$_FILES['files']['name'][$key]}");
        echo "<br><audio preload controls src='uploaded/{$_FILES['files']['name'][$key]}' id='audio-player{$i}' name='audio-player{$i}'></audio><br>";
        $i++;
    }
}
?>


You would need to move your echo statement in to the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜