By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

Multiple file upload error?

rahulephp
P: 59
i think i am missing something in the below script:
It shows error an do not upload files to destination:

Let me know how to solve this:

Expand|Select|Wrap|Line Numbers
  1.   <?php
  2.  
  3.   if (isset($_POST[submit])) 
  4.   {
  5.   $uploadArray= array();
  6.   $uploadArray[] = $_POST['uploadedfile'];
  7.   $uploadArray[] = $_POST['uploadedfile2'];
  8.   $uploadArray[] = $_POST['uploadedfile3'];
  9.  
  10.       foreach($uploadArray as $file) 
  11.       {
  12.           $target_path = "upload/";
  13.           $target_path = $target_path . basename( $_FILES["$file"]['name']);
  14.  
  15.             if(move_uploaded_file($_FILES["$file"]['tmp_name'], $target_path)) 
  16.                 {
  17.                 echo "The file ". basename( $_FILES["$file"]['name'])." has been uploaded";
  18.                 }
  19.            else
  20.                     {
  21.                 echo "There was an error uploading the file, please try again!";
  22.                  }
  23.           }
  24.   }
  25.   ?>
  26.  
  27.   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  28.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  29.   <html xmlns="http://www.w3.org/1999/xhtml">
  30.   <head>
  31.   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  32.   <title>Untitled Document</title>
  33.   </head>
  34.  
  35.   <body>
  36.   <form enctype="multipart/form-data" action="" method="POST">
  37.   <p>
  38.       <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  39.   Choose a file to upload:
  40.   <input name="uploadedfile" type="file" />
  41.   </p>
  42.   <p>Choose a file to upload:
  43.   <input name="uploadedfile2" type="file" />
  44.   </p>
  45.   <p>Choose a file to upload:
  46.   <input name="uploadedfile3" type="file" />
  47.   <br />
  48.   <input name="submit" type="submit" id="submit" value="submit" />
  49.   </p>
  50.   </form>
  51.   </body>
  52.   </html>
  53.  
Error Msg:
Expand|Select|Wrap|Line Numbers
  1. Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 25
  2. There was an error uploading the file, please try again!
  3. Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 22
  4.  
  5. Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 25
  6. There was an error uploading the file, please try again!
  7. Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 22
  8.  
  9. Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 25
  10. There was an error uploading the file, please try again! 
Dec 12 '09 #1

✓ answered by kovik

I think that Bytes.com's [code] tags are glitchy... Your code has seemingly random HTML entities scattered about and, in "Expand" mode, the ampersands are all converted to "&amp;". So, I'll try to ignore the HTML entities when responding. It's also be nice if Bytes.com had syntax highlighting. *ahem* Anyway...


Basically, your main problem seems to be this undefined index. Your error message has no entry for the undefined index, as you can see. This is because it is looking for index "". That's an empty string, and likely not what you were going for. On lines 22 and 25, you are attempting to look at $_FILES["$file"]. $file is defined in your foreach loop as an element of $uploadArray. However, your $uploadArray array consists of $_POST data which is, firstly, nonexistent for your upload form and, secondly, wouldn't be a scalar value (i.e. string, integer) if you were using the correct array, $_FILES, anyway. Therefore, it results in an empty value.


I have two suggestions for you:

1. Make use of form arrays in your form.

Expand|Select|Wrap|Line Numbers
  1. <form action="#" method="post" enctype="multipart/form-data">
  2.     <input type="file" name="files[]" />
  3.     <input type="file" name="files[]" />
  4.     <input type="file" name="files[]" />
  5.     <button type="submit">Upload</button>
  6. </form>
When this is submitted, $_FILES['files'] will be an array of 3 uploaded files, in the order that they exist in the form. This also provides you with the flexibility of having a variable amount of uploaded files, and grants you the ability to add more input fields dynamically (i.e. via JavaScript) without changing any of the back-end code.

2. Traverse the form array for uploading.

Expand|Select|Wrap|Line Numbers
  1. if (!empty($_FILES)) {  // Form has been submitted
  2.     if (!empty($_FILES['files']) && is_array($_FILES['files'])) {  // The file array is formatted correctly
  3.         for ($i = 0; $i < sizeof($_FILES['files']['name']); $i++) {
  4.             // Perform upload with $_FILES['files'][<attribute>][$i]
  5.         }
  6.     }
  7. }
You cannot use a foreach loop when you upload multiple files together in a form array, sadly, because PHP organizes them differently. You'd think that each element of the $_FILES['files'] array would be an individual input field from the form, but instead PHP forces it to conform with the "$_FILES[<name>][<attribute>]" format. This is done for safety reasons, though I personally feel we should at least have an option. Not that my personal opinion matters. :P

Anyway, what this means is that, for example, in order to get the temporary file name of the first uploaded file, you would look at $_FILES['files']['tmp_name'][0]. To get the temporary file name of the second uploaded file, you look at $_FILES['files']['tmp_name'][1]. And so on. This is why we need a for loop, instead of a foreach loop, so that we can use $i as the index to check.

Share this Question
Share on Google+
4 Replies


kovik
Expert 100+
P: 1,044
I think that Bytes.com's [code] tags are glitchy... Your code has seemingly random HTML entities scattered about and, in "Expand" mode, the ampersands are all converted to "&amp;". So, I'll try to ignore the HTML entities when responding. It's also be nice if Bytes.com had syntax highlighting. *ahem* Anyway...


Basically, your main problem seems to be this undefined index. Your error message has no entry for the undefined index, as you can see. This is because it is looking for index "". That's an empty string, and likely not what you were going for. On lines 22 and 25, you are attempting to look at $_FILES["$file"]. $file is defined in your foreach loop as an element of $uploadArray. However, your $uploadArray array consists of $_POST data which is, firstly, nonexistent for your upload form and, secondly, wouldn't be a scalar value (i.e. string, integer) if you were using the correct array, $_FILES, anyway. Therefore, it results in an empty value.


I have two suggestions for you:

1. Make use of form arrays in your form.

Expand|Select|Wrap|Line Numbers
  1. <form action="#" method="post" enctype="multipart/form-data">
  2.     <input type="file" name="files[]" />
  3.     <input type="file" name="files[]" />
  4.     <input type="file" name="files[]" />
  5.     <button type="submit">Upload</button>
  6. </form>
When this is submitted, $_FILES['files'] will be an array of 3 uploaded files, in the order that they exist in the form. This also provides you with the flexibility of having a variable amount of uploaded files, and grants you the ability to add more input fields dynamically (i.e. via JavaScript) without changing any of the back-end code.

2. Traverse the form array for uploading.

Expand|Select|Wrap|Line Numbers
  1. if (!empty($_FILES)) {  // Form has been submitted
  2.     if (!empty($_FILES['files']) && is_array($_FILES['files'])) {  // The file array is formatted correctly
  3.         for ($i = 0; $i < sizeof($_FILES['files']['name']); $i++) {
  4.             // Perform upload with $_FILES['files'][<attribute>][$i]
  5.         }
  6.     }
  7. }
You cannot use a foreach loop when you upload multiple files together in a form array, sadly, because PHP organizes them differently. You'd think that each element of the $_FILES['files'] array would be an individual input field from the form, but instead PHP forces it to conform with the "$_FILES[<name>][<attribute>]" format. This is done for safety reasons, though I personally feel we should at least have an option. Not that my personal opinion matters. :P

Anyway, what this means is that, for example, in order to get the temporary file name of the first uploaded file, you would look at $_FILES['files']['tmp_name'][0]. To get the temporary file name of the second uploaded file, you look at $_FILES['files']['tmp_name'][1]. And so on. This is why we need a for loop, instead of a foreach loop, so that we can use $i as the index to check.
Dec 12 '09 #2

rahulephp
P: 59
Hey thanks for your reply,

I have achived this in folowing way:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $image_count = count($_FILES[image][name]);
  3. //db($_FILES);
  4. for($i = 0; $i < $image_count; $i++)
  5.     {
  6.  
  7.         $src = $_FILES['image']['tmp_name'][$i];
  8.  
  9.        $destination = BASE_DIR . 'upload/' .$_FILES['image']['name'][$i];
  10.         if (move_uploaded_file($src, $destination))
  11.         {
  12.             chmod($destination, 0664);
  13.             $detail[temp_image]=$_FILES['image']['name'];
  14.         }
  15.         else
  16.         {
  17.             die('Sorry, the system was unable to upload the project image.');
  18.         }
  19.     }
  20.  
Dec 14 '09 #3

rahulephp
P: 59
Hey thanks for your reply,

However, I have achieved this in following way,

Expand|Select|Wrap|Line Numbers
  1.  
  2. $image_count = count($_FILES[image][name]);
  3. //db($_FILES);
  4. for($i = 0; $i < $image_count; $i++)
  5.     {
  6.  
  7.         $src = $_FILES['image']['tmp_name'][$i];
  8.  
  9.        $destination = BASE_DIR . 'upload/' .$_FILES['image']['name'][$i];
  10.         if (move_uploaded_file($src, $destination))
  11.         {
  12.             chmod($destination, 0664);
  13.             $detail[temp_image]=$_FILES['image']['name'];
  14.         }
  15.         else
  16.         {
  17.             die('Sorry, the system was unable to upload the project image.');
  18.         }
  19.     }
  20.  
  21. $image=$detail[temp_image];
  22.  
  23.  
Thanks again for your response.
Dec 14 '09 #4

kovik
Expert 100+
P: 1,044
Why do you reassign $detail['temp_image'] every time a file is successfully uploaded?

Also, don't forget to wrap your array keys in quotation marks. When you don't, then PHP assumes that you meant a constant. It wastes time looking for that constant before determining that you meant a string. And, if it does find a constant, it will overwrite your string.

Like this:
$detail[temp_image] should be $detail['temp_image']
$_FILE[image][name] should be $_FILES['image']['name']
Dec 14 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.