OtagoHarbour Posted December 11, 2013 Share Posted December 11, 2013 I am trying to modify the code given here to upload multiple files. The code I use is as follows. ?> <form action="uploadFiles.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file[]" multiple><br> <input type="submit" name="submit" value="Submit"> </form><? uploadFiles.php has the following code <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"][0] . "<br>"; } else { echo "No. files uploaded : ".count($_FILES['file']['name'])."<br>"; echo "Upload: " . $_FILES["file"]["name"][0] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; }?> I select 2 files in the browser window and get the following output. No. files uploaded: 0Upload:Type:Size: 0kBStored in: I cannot understand why I am getting 0 files when I selected 2 files. Thanks, OH. Link to comment Share on other sites More sharing options...
Don E Posted December 11, 2013 Share Posted December 11, 2013 When you select multiple files in the way you're doing it, the "name", "type", etc are arrays and those arrays indexes will contain the info you're looking for.By adding a [0] to the code below, you'll access the first index of that array: if ($_FILES["file"]["error"][0] > 0) { echo "Error: " . $_FILES["file"]["error"][0] . "<br>"; // this would check for error only for that first image selced } else { echo "No. files uploaded : ".count($_FILES['file']['name'])."<br>"; echo "Upload: " . $_FILES["file"]["name"][0] . "<br>"; // this selects the name of the first image selected echo "Type: " . $_FILES["file"]["type"][0] . "<br>";// this selects the type of the first image selected echo "Size: " . ($_FILES["file"]["size"][0] / 1024) . " kB<br>"; // this gets the size of the first image selected echo "Stored in: " . $_FILES["file"]["tmp_name"][0]; // // this gets the temp_name of the first image selected } If you do a print_r on $_FILES["file] like this: print_r($_FILES["file"]) and go look in the source code of the web page, you'll get a better idea of how the arrays look. Here's an example:Array( [name] => Array ( [0] => 400lomoupmi.png [1] => 400saraship.png ) [type] => Array ( [0] => image/png [1] => image/png ) [tmp_name] => Array ( [0] => /private/var/folders/f8/fswjbfl94cj77tbw053hcdb40000gn/T/phpnTzAq0 [1] => /private/var/folders/f8/fswjbfl94cj77tbw053hcdb40000gn/T/phpV97ABJ ) [error] => Array ( [0] => 0 [1] => 0 ) => Array ( [0] => 213597 [1] => 181958 )) I selected 2 images as you can see. You can use a foreach loop to loop through the arrays to get the values for each to display on the webpage. Link to comment Share on other sites More sharing options...
OtagoHarbour Posted December 11, 2013 Author Share Posted December 11, 2013 When you select multiple files in the way you're doing it, the "name", "type", etc are arrays and those arrays indexes will contain the info you're looking for.By adding a [0] to the code below, you'll access the first index of that array: if ($_FILES["file"]["error"][0] > 0) { echo "Error: " . $_FILES["file"]["error"][0] . "<br>"; // this would check for error only for that first image selced } else { echo "No. files uploaded : ".count($_FILES['file']['name'])."<br>"; echo "Upload: " . $_FILES["file"]["name"][0] . "<br>"; // this selects the name of the first image selected echo "Type: " . $_FILES["file"]["type"][0] . "<br>";// this selects the type of the first image selected echo "Size: " . ($_FILES["file"]["size"][0] / 1024) . " kB<br>"; // this gets the size of the first image selected echo "Stored in: " . $_FILES["file"]["tmp_name"][0]; // // this gets the temp_name of the first image selected } If you do a print_r on $_FILES["file] like this: print_r($_FILES["file"]) and go look in the source code of the web page, you'll get a better idea of how the arrays look. Here's an example:Array( [name] => Array ( [0] => 400lomoupmi.png [1] => 400saraship.png ) [type] => Array ( [0] => image/png [1] => image/png ) [tmp_name] => Array ( [0] => /private/var/folders/f8/fswjbfl94cj77tbw053hcdb40000gn/T/phpnTzAq0 [1] => /private/var/folders/f8/fswjbfl94cj77tbw053hcdb40000gn/T/phpV97ABJ ) [error] => Array ( [0] => 0 [1] => 0 ) => Array ( [0] => 213597 [1] => 181958 )) I selected 2 images as you can see. You can use a foreach loop to loop through the arrays to get the values for each to display on the webpage. Thank you for your reply but it still does not appear to be working properly. I tried echo "_FILES[file]: " . print_r($_FILES['file']) . "<br>"; if ($_FILES["file"]["error"][0] > 0) { echo "Error: " . $_FILES["file"]["error"][0] . "<br>"; // this would check for error only for that first image selced } else { echo "No. files uploaded : ".count($_FILES['file']['name'])."<br>"; echo "Upload: " . $_FILES["file"]["name"][0] . "<br>"; // this selects the name of the first image selected echo "Type: " . $_FILES["file"]["type"][0] . "<br>";// this selects the type of the first image selected echo "Size: " . ($_FILES["file"]["size"][0] / 1024) . " kB<br>"; // this gets the size of the first image selected echo "Stored in: " . $_FILES["file"]["tmp_name"][0]; // // this gets the temp_name of the first image selected } and got _Files[file]: 1No. files uploaded: 0Upload:Type:Size: 0 kBStored in: Link to comment Share on other sites More sharing options...
Don E Posted December 11, 2013 Share Posted December 11, 2013 (edited) Here's what I did and it worked for me. The page submits to it self instead of going to another page. This isnt necessary but I just did it that way so I did not have to make two files. <?php if(isset($_POST['submit'])){ if ($_FILES["file"]["error"][0] > 0) { echo "Error: " . $_FILES["file"]["error"][0] . "<br>"; } else { echo "No. files uploaded : ".count($_FILES['file']['name'])."<br>"; echo "Upload: " . $_FILES["file"]["name"][0] . "<br>"; echo "Type: " . $_FILES["file"]["type"][0] . "<br>"; echo "Size: " . ($_FILES["file"]["size"][0] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"][0]; }}?><!doctype html><html><head><meta charset="UTF-8"><title>mult upload</title></head><body><form action="multupload.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file[]" multiple><br> <input type="submit" name="submit" value="Submit"> </form></body></html> This is the result when selecting two images but since we're only viewing the [0], we get info just for that image stored at that array index:No. files uploaded : 2Upload: 700wbmwm3race.pngType: image/pngSize: 251.8955078125 kBStored in: /private/var/folders/f8/fswjbfl94cj77tbw053hcdb40000gn/T/phptyQ0jE Edited December 11, 2013 by Don E 1 Link to comment Share on other sites More sharing options...
OtagoHarbour Posted December 12, 2013 Author Share Posted December 12, 2013 (edited) I tried that code except without <!doctype html> since it is a PHP file. I found that if(isset($_POST['submit'])) returns false. I don't know why this is since this is my form code and I selected 2 files. <form action="uploadFiles.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file[]" multiple /><br> <input type="submit" name="submit" value="submit" /></form> Thanks, OH Edited December 13, 2013 by OtagoHarbour Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now