473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 4082
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 the file in the tempoary area. And i'm not sure...
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 input boxes using "<input type="file" size="40"...
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, not just files. So I have the file upload and...
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 form like the following using libcurl: <form...
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 image and create a thumb of the image in another...
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 upload more than four meg, i get an error message...
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 multiple files. I know the path of the files, i...
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 multiple file upload page, with some nice progress...
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 directorys and upload those to the server. Another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.