1

I am trying to upload multiple photos at the same time but there seems to be an error with my script. If for example, I select 10 different photos, one particular image is uploaded 10 times (ignoring the other 9 images). This is the script:

for ($i = 0; $i < count($_FILES["_photo"]["name"]); $i++) {  
    if (!empty($_FILES['_photo']['name'][$i])) {

    if (($_FILES['_photo']['type'][$i] == 'image/jpeg') OR ($_FILES['_photo']['type'][$i] == 'image/png') OR ($_FILES['_photo']['type'][$i] =='image/gif')) {
        $upload_folder = "./profile_pix/";
        $pic_name = time() . ".jpg";
        $pic_path = $upload_folder . $pic_name;

        require_once "include/resize.php";
        if (move_uploaded_file($_FILES['_photo']['tmp_name'][$i], $pic_path)) {
            $image = new Resize($pic_path);
            $image->resizeImage(180, 180, 'crop');
            $image->saveImage($pic_path);

        }
            $sql2 = "INSERT INTO photos
            (photo, member_id, photo_id)
             VALUES
            ('$pic_name', :session_id, :offer_count)";
            $db -> query($sql2, array('session_id' => $_SESSION['id'], 'offer_count' => $offer_count));
    }else {
            header ("Location: submitoffer.php?err=03");
        }


    }

HTML:

<input type="file" id="_photo"  name="_photo[]" multiple="multiple">
Mihai
  • 23,092
  • 7
  • 58
  • 73
shekwo
  • 1,164
  • 1
  • 12
  • 31

3 Answers3

1

File upload is working fine.

The line

$pic_name = time() . ".jpg";

is always evaluating to same value.

As logically all files are getting uploaded on same time().

Instead use:

$pic_name = $i . '_'. time() . ".jpg";

To add uniqueness.

Pupil
  • 23,141
  • 5
  • 40
  • 62
  • I could argue using `uniqid()` as replacement for time(), but that still doesn't say why the same photo is uploaded 10 times ;) – Raphioly-San Jul 08 '15 at 12:14
  • Probably it uploads all files with the same name since time returns unix timestamp which is in seconds and php can easily upload 10 or more files in under a second – Mihai Jul 08 '15 at 12:16
0

try this, and you will see what is happening:

<?php
    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
?>

And in your HTML, make sure you use different names, like this:

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

Then pick them up by each name. Just inspect the content of $_FILES to understand the structure.

I also advice you serious do some error checking.

Raphioly-San
  • 385
  • 2
  • 10
Erwin Moller
  • 2,239
  • 11
  • 20
  • It should have an array as name in HTML – Mihai Jul 08 '15 at 12:11
  • I changed it to an array, but that isn't necessary. You can also name them differently. I prefer your suggestion above giving them different names, so there you go! – Erwin Moller Jul 08 '15 at 12:19
0

100% working and tested code:

upload.php:

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
  if(isset($_FILES['files'])){
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
      $file_name  = $_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_type == "image/jpg" || $file_type == "image/png" || $file_type == "image/jpeg" || $file_type == "image/gif" ) {      
        move_uploaded_file($file_tmp,"uploads/".$file_name);        
      }   
    }
    header('location:uploads.html'); 
  }
}
else { header('location:404.html'); }
?>

upload.html

<form id="upload_images" action="upload.php" method="POST"  enctype="multipart/form-data">
      <input type="file" name="files[]" id="file" multiple="true" />                
      <input type="submit" id="btn_upload"  value="U P L O A D" />
</form>
Shahzad
  • 2,443
  • 6
  • 21
  • 31