如何在上传后显示多个图像?

时间:2014-05-08 10:07:08

标签: php file-upload

我查找了多个文件上传脚本,所以我偶然发现了这个:http://techstream.org/Web-Development/PHP/Multiple-File-Upload-with-PHP-and-MySQL我知道这是一个旧帖子,但它工作得非常好。我删除了SQL部分因为我不需要它。因此,当您查看脚本时,它只会将多个文件上传到服务器,我尝试显示上传的图像,如下所示:

if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }
        $desired_dir="../img/full-size";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{                                  // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }       
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
        echo "<img src='" . $desired_dir . '/' . $file_name . "'>";
        echo "<img src='" . $desired_dir . '/' . $file_name . "'>";

    }
}

HTML:

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple/>
    <input type="submit"/>
</form>

这样可行,但仅限于一个Image,而不是数组。有什么帮助吗?

3 个答案:

答案 0 :(得分:1)

你在评论中说:

  

输出只是一次图像两次

当然是这样,因为你这样做了:

for each uploaded image
    move image somewhere
end for
if no errors
    show 2 same images (it will be last images shown 2 times)
end if

你做这个部分:

if(empty($error)){
    echo "Success";
    echo "<img src='" . $desired_dir . '/' . $file_name . "'>";
    echo "<img src='" . $desired_dir . '/' . $file_name . "'>";
}

不是为了,而是在它之后。您的代码输出上次上传图像2次。

您需要将输出放在for in循环中(使用图像链接数组更新代码):

$images = array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_FILES['files']['name'][$key];
    $file_size =$_FILES['files']['size'][$key];
    $file_tmp =$_FILES['files']['tmp_name'][$key];
    $file_type=$_FILES['files']['type'][$key];  
    if($file_size > 2097152){
        $errors[]='File size must be less than 2 MB';
    }
    $desired_dir="../img/full-size";
    if(empty($errors)==true){
        if(is_dir($desired_dir)==false){
            mkdir("$desired_dir", 0700);        // Create directory if it does not exist
        }
        if(is_dir("$desired_dir/".$file_name)==false){
            move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
        }else{                                  // rename the file if another one exist
            $new_dir="$desired_dir/".$file_name.time();
             rename($file_tmp,$new_dir) ;               
        }  
        $images[] ="<img src='" . $desired_dir . '/' . $file_name . "'>"; //  <----- there    
    }else{
            print_r($errors);
    }
}

foreach($images as $image) {
     echo $image;
}

答案 1 :(得分:0)

我认为您必须在上传后显示文件夹中的图片,请尝试以下操作:

删除此代码:

if(empty($error)){
        echo "Success";
        echo "<img src='" . $desired_dir . '/' . $file_name . "'>";
        echo "<img src='" . $desired_dir . '/' . $file_name . "'>";

    }

并添加此

if(empty($error)){

    $files = glob("../img/full-size/*.*");
    for ($i=1; $i<count($files); $i++)
    {

      $num = $files[$i];
      echo '<img src="'.$num.'" align="absmiddle" /> ';
      print substr(substr($num,6,100),0,-4);

    }

}

答案 2 :(得分:0)

试试,

<?php 
if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }
        $desired_dir="ne_img";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir");
                chmod("$desired_dir", 0755);       // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name) ;
                $files_name[] = $file_name;
            }else{                                  // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }       
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        if(is_array($files_name))
        {
            foreach($files_name as $file_name)
            {
                echo "<img src='" . $desired_dir . '/' . $file_name . "'>";
            }
        }


    }
}

?>