在PHP中图像上传和重命名时出错

时间:2015-04-20 05:58:40

标签: php image-uploading

表格

<form action="product.php" method="post" enctype="multipart/form-data"> 
<input type="file" name="image" class="input3" multiple>
 <input type="submit" value="Add Product" name="submit" class="button2">
</form>

product.php

$image = $_FILES['image'];
 $i = 1 ;
 foreach ($image as $new_image)
    {
       print_r($new_image);
       echo '<br>';
       $dir_path_up = 'assets/images/product_images/'.$model."/";
       $target_file = $dir_path_up . basename($new_image);
       $new_name= $dir_path_up .$i.".jpg";
       move_uploaded_file($_FILES["image"]["tmp_name"], $new_name);

       $i++;
    } 

我使用上面的代码重命名图像并将图像上传到特定的($ model)目录。上传单张图片时,一切看起来都很完美。但是当我上传超过单张图像时,最后一张图像只能重命名和上传。 (示例:如果我上传3张图片,则上传图片只会上传,并且会重命名为1.jpg。之前的其他图片都没有上传。)

这有什么不妥吗?我得到了Struck。

2 个答案:

答案 0 :(得分:3)

当您上传图像时,上传的图像只会被上传,原因是您没有正确处理它。在此之前尝试print_r($_FILES)以查看实际结构。

  1. 您需要使用

    <input type="file" name="image[]" />

  2. 您应该拥有的样本结构:

  3. 阵 (

    [image] => Array
        (
    
            [name] => Array
                (
    
                    [0] => architectural-245a.jpg
    
                    [1] => BeaverMeadow_EN-US12190942812_1920x1200.jpg
    
                    [2] => cool-chelsea-wallpaper-25403-26085-hd-wallpapers.jpg
                )
    
            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                )
    
            [tmp_name] => Array
                (
                    [0] => D:\xampp\tmp\phpE4FF.tmp
                    [1] => D:\xampp\tmp\phpE52F.tmp
                    [2] => D:\xampp\tmp\phpE54F.tmp
                )
    
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )
    
            [size] => Array
                (
                    [0] => 689711
                    [1] => 642453
                    [2] => 396336
                )
    
        )
    

    然后你可以适当地处理它。

答案 1 :(得分:1)

试试这段代码:

第一个问题是您上传文件时使用的input(type= file)字段的名称。您的表单实际上只上传了一个最终选中的文件。这就是你面临这个问题的原因。

因此解决此问题将其重命名为image[]

   <form action="" method="post" enctype="multipart/form-data"> 
        <input type="file" name="image[]" class="input3" multiple> <!-- As you are uploading multiple file so u have to define name as array like =image[] -->
         <input type="submit" value="Add Product" name="submit" class="button2">
     </form> 

   <?php

    if(isset($_POST['submit'])){
        $image = $_FILES['image'];
        $image = reArrayFiles($image);  /* use this function to create a proper $_FILES['image'] array format*/
         $i = 1 ;
         foreach ($image as $new_image)
            {
               print_r($new_image);
               echo '<br>';
               $dir_path_up = "upload";
               $target_file = $dir_path_up . basename($new_image['name']);
               $new_name= $dir_path_up .$i.".jpg"; /*if you use .jpg here then all the files will be converted into .jpg even if user uploads a txt file or other*/
               move_uploaded_file($new_image["tmp_name"], $new_name);
               $i++;
            }
    }

    function reArrayFiles(&$file_post) {

        $file_ary = array();
        $file_count = count($file_post['name']);
        $file_keys = array_keys($file_post);

        for ($i=0; $i<$file_count; $i++) {
            foreach ($file_keys as $key) {
                $file_ary[$i][$key] = $file_post[$key][$i];
            }
        }

        return $file_ary;
    }
    ?>