472,326 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,326 software developers and data experts.

Multiple file upload error?

rahulephp
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.

4 3948
kovik
1,044 Expert 1GB
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
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
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
1,044 Expert 1GB
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

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

Similar topics

2
by: dave | last post by:
Hello, I'm trying to get the below script working, adapted from the manual on php.net. Although i am getting success file uploading, i do not see...
1
by: Avin Patel | last post by:
Hi, I am looking for script to allow multiple files can be uploaded/attached in webform ( mostly cgi/perl or php). But I don't like the multiple...
5
by: Shawn H. Mesiatowsky | last post by:
I am creating an intranet App that is a document management system, and now I have been told they wan't the ebilty to version control directories,...
0
by: Yandos | last post by:
Hello all, I'm sorry for a bit off-topic post, but curl does not have own newsgroup, so I hope someone might help me here... I need to feed...
0
Atli
by: Atli | last post by:
Hi. I'm trying to use php code I wrote last year when I was using Apache, so I'm pretty sure the code works. Its a simple code to upload an...
0
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
I have a page with four <input type=filecontrols on it. When i upload less than four meg across all four inputs, the files upload correctly. When i...
7
by: der_grobi | last post by:
That is the Problem: I have an ASP.NET Webapplicatipon where I can upload single files to the Server. That works fine. But now, I want to Upload...
5
by: John Devlon | last post by:
Hi, Some people like to go on vacation during christmas time, others try to do something they never did before.... I would like to create a...
9
by: torso | last post by:
Hi Does someone know a good tutorial for multiple file upload with xmlHttpRequest. I am trying to do directory upload. So I could choose...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.