Jump to content
Sign in to follow this  
OtagoHarbour

Selecting and Uploading Multiple Files

Recommended Posts

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.

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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:

Share this post


Link to post
Share on other sites

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 by Don E
  • Like 1

Share this post


Link to post
Share on other sites

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 by OtagoHarbour

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...