【问题标题】:Display Multple Uploaded Images on page with PHP使用 PHP 在页面上显示多个上传的图像
【发布时间】:2014-05-02 09:52:38
【问题描述】:

好的,这就是我目前所得到的,我能够上传一张图片并将其显示在一个 div 中,但我似乎无法弄清楚如何为加载多张图片做同样的事情。就像上传多张图片并在屏幕上显示所有上传的图片一样。任何帮助表示赞赏,在此先感谢。

另外:我觉得我必须为

设置一个变量

$_FILES['image']['name'][0] $_FILES['图像']['名称'][1] 等等

然后做一个forloop把它打印出来?如果我错了,请纠正我?

<?php
// prevent timezone warnings
date_default_timezone_set('America/New_York');

// set the upload location
$UPLOADDIR = "tmp";

// if the form has been submitted then save and display the image(s)
if(isset($_POST['Submit'])){
    // loop through the uploaded files
    foreach ($_FILES as $key => $value){
        $image_tmp = $value['tmp_name'];
        $image = $value['name'];
        $image_file = "{$UPLOADDIR}{$image}";

        // move the file to the permanent location
        if(move_uploaded_file($image_tmp,$image_file)){
            echo <<<HEREDOC

<div style="float:left;margin-right:10px">
    <img src="{$image_file}" alt="file not found" /></br>
</div>

HEREDOC;
        }
        else{
            echo "<h1>image file upload failed, image too big after compression</h1>";
        }
    }
}
else{
    ?>

<form name='newad' method='post' enctype='multipart/form-data' action=''>
    <table>
    <tr>
        <td><input type='file' name='image'></td>
    </tr>
    <tr>
        <td><input name='Submit' type='submit' value='Upload image'></td>
    </tr>
</table>
</form>

<?php
}
?>

【问题讨论】:

    标签: php


    【解决方案1】:

    我不确定这是哪个 CMS/框架,但如果您更改此行

    来自

    <input type='file' name='image'>
    

    收件人

    <input type='file' name='image[]' multiple>
    

    希望对你有所帮助。

    【讨论】:

    • 我试过这是我得到的...警告:move_uploaded_file() 期望参数 1 是字符串,数组在第 40 行的 C:\sandbox\array\part2.php 中给出 - 第 40 行是 if(move_uploaded_file($image_tmp,$image_file)){
    • 我帮不了那么多,因为它的代码很长。您可以查看此网站developphp.com/view.php?tid=1395,该网站也有关于此的视频教程。
    【解决方案2】:

    怎么有多个文件,你必须有两个循环。将foreach 放入for。 我相信这就是你想要的。确保tmp 文件夹有写权限。

    <?php
    // prevent timezone warnings
    date_default_timezone_set('America/New_York');
    
    // set the upload location
    $UPLOADDIR = "tmp";
    
    // if the form has been submitted then save and display the image(s)
    if(isset($_POST['Submit'])){
    
        $num_files = count($_FILES['image']['tmp_name']);
    
        for($x = 0; $x < $num_files; $x++){
            $image = $_FILES['image']['name'][$x];
            $image_file = $UPLOADDIR."/". $image;
            if(!is_uploaded_file($_FILES['image']['tmp_name'][$x])){
                $messages[] = '<h1>'.$image.' image file upload failed, image too big after compression</h1>."<br>"';
            }
            if (move_uploaded_file($_FILES["image"]["tmp_name"][$x],$image_file)){
                echo '
                <div style="float:left;margin-right:10px">
                    <img src="'.$image_file.'" alt="file" /></br>
                </div>';
            } else{
                echo "<h1>image file upload failed, image too big after compression</h1>";
            }
        }
    }else {
        ?>
    
    <form name='newad' method='post' enctype='multipart/form-data' action=''>
        <table>
        <tr>
            <td><input type='file' name='image[]'></td>
        </tr>
        <tr>
            <td><input type='file' name='image[]'></td>
        </tr>
        <tr>
            <td><input name='Submit' type='submit' value='Upload image'></td>
        </tr>
    </table>
    </form>
    
    <?php
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 2018-10-20
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多