Error in image uploading and renaming in php

614

Form

<form action="product.php" method="post" enctype="multipart/form-data"> 
<input type="file" name="image" multiple>
 <input type="submit" value="Add Product" name="submit" >
</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++;
    } 

I used above code to Rename image and upload image to specific($model) directory. All looks perfect, when upload a single image. But when I upload more than Single Image, last image only get rename and Upload. (Example : If i upload 3 image, last image get only upload and it get rename as1.jpg. and rest of previous images are not getting upload.)

Is some thing wrong in this?? I get Struck on this.

938

Answer

Solution:

When you are uploading the images the last image will be uploaded only the reason being you are not handling it correctly. Tryprint_r($_FILES) before this to see the actual structure.

  1. You need to use

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

  2. The sample structure you should have:

Array (

[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
            )

    )

)

Then you can handle it suitably.

540

Answer

Solution:

Try this code:

First problem is with name of theinput(type= file) field you have taken for uploading file. your form is actually uploading only one file which was selected in the end. That is why you are facing that problem.

So solve this rename it asimage[]

  <form action="" method="post" enctype="multipart/form-data"> 
    <input type="file" name="image[]" 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" >
   </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;
  }
  ?>

People are also looking for solutions to the problem: php - OpenCart - not sending emails form default contact page

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.