PHP upload: upload multiple files?

I am currently uploading a single file as follows:

<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> 

And with the following script:

 <?php error_reporting(E_ALL); if (($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; $moved = move_uploaded_file($_FILES["file"]["tmp_name"], "C:/inetpub/wwwroot/PHP_Ramp/upload/" . $_FILES["file"]["name"]); if ($moved) { echo "Move: Success <br/>"; } else { echo "Move Failed <br/>"; } echo "Stored in: " . "C:/inetpub/wwwroot/PHP_Ramp/upload/" . $_FILES["file"]["name"]; } } else { echo "Invalid file"; } ?> 

Now I am trying to allow the user to select three different files from the form. I found several tutorials that show how to do something like this, but I can't get it to work.

I changed the form as follows (including three inputs):

 <html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <input type="file" name="file" id="file" /> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> 

But I'm not sure how to change php to handle all three files. I know that I need to iterate over _FILES, but everything I tried does not work. Any pointers would be appreciated.

+1
source share
5 answers

Each element of the file must have a unique name or use the shorthand of the PHP array:

 <input type="file" name="file1" /> <input type="file" name="file2" /> 

or

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

Remember - the name attribute determines how you will identify this field on the server, and if you use the same name several times, PHP will overwrite previous copies with the last one when it analyzes the presented data. The array designation ( [] ) tells PHP that you are INTENDED to have multiple fields with the same name and that each found copy should be added to the array, not overwritten.

For a unique version of the name, you will process each, as of now, with a single file.

For the array version, PHP has a design dullness that requires slightly different processing. You get an array of $ _FILES that looks like

 $_FILES = array( 'fieldname' => array( 'name' => array( 0 => 'first file', 1 => 'second file', etc... ) ) ) 
+7
source

You need to change

 <input name="file" /> 

to

 <input name="file[]" /> 

To turn them into an array.

Then in the script you refer to them as:

 $_FILES['file']['name'][0]; // first file $_FILES['file']['name'][1]; // second file 

Please note that you can replace the name with any of the other properties of the file, which is usually used in one file (for example, size, type, etc.).


Alternatively, you can give them all different names:

 <input name="firstfile" /> <input name="secondfile" /> 

Then in the script:

 $_FILES['firstfile']; // first file $_FILES['secondfile']; // second file 
+4
source

You can select multiple files in one input like this.

 <input type="file" name="pic[]" id="pic" accept="image/*" multiple="multiple"/> 

This input field can accept multiple files by pressing the control key. Now you can access them in php, for example,

 $_FILES['file']['name'][0]; // first file $_FILES['file']['name'][1]; // second file 
+1
source

You need to change the value of the name attribute on the HTML side to file[] , and then in PHP just iterate through $_FILES['files'] and process each element, as usual, with one file.

0
source

KISS code with some explanations:

Below is the full functional simple code to get started. You can add error checking, maximum size material, etc. After you get something simple.

The $ _FILES array is a three-dimensional array built into PHP that stores all the information about the downloaded file.

The HTML INPUT tag will have only one element for each <name = 'part.

So, if your INPUT tag looks like this:

 <INPUT name="InpFile[]" type="file" /> 

In the $ _FILES array there will be one element of the top array named "InpFile".

Also notice the [] after InpFile ..... which tells PHP that every time you use an input tag with the same "name", it adds it to the same top element of the $ _FILES array.

Inside this one element there are 5 other elements of the array with the names: 'name', 'type', 'tmp_name', 'error' and 'size'.

And each of these array elements will contain data for each uploaded file.

Your first name of the downloaded file will be in $ _FILES ['InpFile'] ['name'] ['0']

And other information about your first uploaded file will also be the same in the array, for example, the size of the first file will be in $ _FILES ['InpFile'] ['size'] ['0']

Each subsequent file name will be in $ _FILES ['InpFile'] ['name'] [1], $ _FILES ['InpFile'] ['name'] [2] .... etc

After downloading, each file will have a random temporary name in the element $ _FILES ['InpFile'] ['tmp_name'] [0 ... n], which is the name that it uses for the first file upload to the time domain.

SO, after downloading, you must move the files from the time domain to where you want.

This is done using this statement:

 move_uploaded_file($_FILES['InpFile']['tmp_name'][$Key], $_FILES['InpFile']['name'][$Key] ) or die("Move from Temp area failed"); 

In the description below, foreach $ Key and $ Name are only available so that $ Key gets all increasing numbers for each iteration ... i.e. 0, 1, 2 ..... etc., and then you can use $ Key to refer to the name, tmp_name, etc. Each file in the array.

This code allows you to do everything on one page, since the form actually calls itself (action = "") for publication. Therefore, the first time you load the page, you will get an error in the php code because $ _FILES is not installed yet ... therefore all the code is in the If: If ($ _FILES) statement.

After you submit the form, it will do this and then repeat the status for each file after it is moved to your area.

In addition, this code will upload the file to the same directory as the page ... you can change everything using information from other messages in SO.

  <FORM action="" method="post" enctype="multipart/form-data"> <P align="center"><B>Choose Files</B><BR> <BR> File One: <INPUT name="InpFile[]" type="file" /> <BR> <BR> File Two: <INPUT name="InpFile[]" type="file" /> <BR> </P> <P align="center"><BR> <INPUT type="submit" name="submit" value="UpLoad"> </P> </FORM> <H3 align="center">&nbsp;</H3> <H3 align="center">Status:</H3> <P align="center">&nbsp;</P> <P align="center"> 

Put this PHP code right here on the same page:

 <?php If ($_FILES) { foreach ($_FILES ['InpFile']['name'] as $Key => $Name) { move_uploaded_file( $_FILES['InpFile']['tmp_name'][$Key], $_FILES['InpFile']['name'][$Key] ) or die("Move from Temp area Failed"); $EFileName = $_FILES['InpFile']['name'][$Key]; echo "<P>$EFileName: Uploaded"; } } ?> 
0
source

All Articles