PHP 使用 echo 和

发布于 2024-12-02 08:47:50 字数 853 浏览 1 评论 0原文

我的 html5 表单可以使用一个提交按钮上传多个 mp3 文件,但我希望它们全部立即显示(嵌入)在带有标签的页面上。目前我只能得到一个。

问:如何设置显示其他 id(audio-player2、audioplayer3 等)的 echo?

我尝试用相同的东西添加第二个回声并替换audio-player2,但它(可以理解)播放相同的文件。大概是因为 (name="files[]")

这里是 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>";
}
?>

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>";
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-12-09 08:47:50

将 echo 语句移至 foreach 循环内并添加计数变量:

<?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++;
    }
}
?>

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++;
    }
}
?>
眼泪都笑了 2024-12-09 08:47:50

您需要将 echo 语句移至循环中。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文