1

I'm uploading images using multiple instances of the following file input:

<input type="file" name="photos[]">

I've set the form properties like this:

<form action="?action=form" method="post" class="nice" enctype="multipart/form-data">

When I loop through the files array I can print out the multiple file names.

But as soon as I try to upload the files it only uploads the first file from the array.

Here is my PHP:

$uploadDir = '/uploads/';

$getCurrentTimeStamp = date('m-d-Y_h.i.s', time());

// Set the allowed file extensions
$fileTypes = array('jpg', 'jpeg', 'png', 'gif'); // Allowed file extensions

$theIds = $_POST["id"];

function findexts ($filename) 
 { 
 $filename = strtolower($filename) ; 
 $exts = split("[/\\.]", $filename) ; 
 $n = count($exts)-1; 
 $exts = $exts[$n]; 
 return $exts; 
 } 

 //This applies the function to our file  

 $fileCount = 1;
foreach ($_FILES["photos"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {


 $ext = findexts ($_FILES['photos']['name'][$key]) ; 

if (!empty($_FILES)) {
    $tempFile   = $_FILES['photos']['tmp_name'][$key];
    $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;

    $targetFile = $uploadDir . "equipment_photo_" .$theIds . "_".$fileCount."_". $getCurrentTimeStamp."." .$ext;
    $theFileNameToStore = "equipment_photo_" .$theIds . "_".$fileCount."_". $getCurrentTimeStamp."." .$ext;

    // Validate the filetype
    $fileParts = pathinfo($_FILES['photos']['name'][$key]);



    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

        // Save the file
        move_uploaded_file($tempFile,$targetFile);

        echo $theFileNameToStore;

    } else {

        // The file type wasn't allowed
        echo 'Invalid file type.';

    }
}


        }


        $fileCount ++;

}

Any ideas why the multiple images will echo but the files won't upload?

5
  • small note: if you just drop $targetFile one line, instead of duplicating the file name, you can just do $targetFile = $uploadDir . $theFileNameToStore Commented Dec 28, 2012 at 22:26
  • and shouldn't you be iterating through $_FILES['photos'], not $_FILES['photos']['error'] ? Commented Dec 28, 2012 at 22:27
  • I think you need a count for loop and set the max files allowed Commented Dec 28, 2012 at 22:28
  • Once the $_FILES array exists all uploads are done already. If you can loop through the file names you have a mistake in your processing. Could you post the output of var_dump( $_FILES ); with more than one file selected? Commented Dec 28, 2012 at 23:15
  • Here is the output of var_dump( $_FILES ); array(1) { ["photos"]=> array(5) { ["name"]=> array(2) { [0]=> string(14) "paper_swan.jpg" [1]=> string(16) "wooden_walls.jpg" } ["type"]=> array(2) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" } ["tmp_name"]=> array(2) { [0]=> string(36) "/Applications/MAMP/tmp/php/phpyFPeIu" [1]=> string(36) "/Applications/MAMP/tmp/php/phpRH6N2I" } ["error"]=> array(2) { [0]=> int(0) [1]=> int(0) } ["size"]=> array(2) { [0]=> int(413005) [1]=> int(1007252) } } } Commented Dec 29, 2012 at 4:59

1 Answer 1

1

To make your life easier, I'd suggest you to use this function to reorder the $_FILES global

function rotate_array($source_array, $keep_keys = TRUE)
{
  $new_array = array();

  foreach ($source_array as $key => $value)
  {
     $value = ($keep_keys === TRUE) ? $value : array_values($value);
     foreach ($value as $k => $v)
     {
        $new_array[$k][$key] = $v;
     }
  }

  return $new_array;
}

Then use it in your code:

$fileCount = 1;
$files = rotate_array($_FILES['photos']);

foreach ($files as $file) {
  if (is_uploaded_file($file['tmp_name'])) {
    $ext = findexts($file['name']); 

    $tempFile   = $file['tmp_name'];
    $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;

    $theFileNameToStore = "equipment_photo_" .$theIds . "_".$fileCount."_". $getCurrentTimeStamp."." .$ext;
    $targetFile = $uploadDir . $theFileNameToStore;

    // Validate the filetype
    $fileParts = pathinfo($file['name']);

    if (in_array(strtolower($fileParts['extension']), $fileTypes)) {
      // Save the file
      //move_uploaded_file($tempFile,$targetFile);
      // Sometimes move won't work for unknown reasons, try copying, this should work
      copy($tempFile, $targetFile);

      echo $theFileNameToStore;
    } else {
      // The file type wasn't allowed 
      echo 'Invalid file type.';
    }
  }
  $fileCount ++;
}

make sure your phpinfo() shows the correct value for max_file_uploads, upload_max_filesize and post_max_size. Change either on your code, or inside your php.ini file

Sign up to request clarification or add additional context in comments.

5 Comments

This definitley cleans things up. Thanks. But I'm still only getting 1 of my images to upload.
even with the php.ini configuration?
Yes. My max_file_uploads is set to 20 and the others are well above what my files need.
I forgot that I too had this problem once, and was because I was trying to move the file instead of copying, the files were getting deleted after being moved, god knows why. So I've updated the code, try that, it should work now
I tried it with Copy instead and it is still only uploading 1 of the files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.