Jump to content

How does $_FILES array look like?


arianhojat

Recommended Posts

I dont understand how the FILES array looks like. is it a multiple dimension 3D array?...
The reason why i think this is to visualize the array, I imagine the name of the inputs going across X axis. so if the html for multiple upload page looks like...
[code]...
<input type="file" name="avatar" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="resume" />
...[/code]

The visualized array, in 'grid-like' format, then to me looks like:
Going across X axis is... "avatar", "pictures", and "resume"
Then going across the Y axis is the type of values it can hold like... "name", "type", "size", "tmp_name", "error"
then going up Z axis is the value, so $_FILES["avatar"]["size"][0] holds the size of that 1 'avatar' input and since it is only value then it has no more going up Z axis. and the Error for this file is in $_FILES["avatar"]["error"][0].

For the pictures, there are the values for the size in...
$_FILES["pictures"]["size"][0]
$_FILES["pictures"]["size"][1]
$_FILES["pictures"]["size"][2]

So just to clarify, the errors for these uploads are in:
$_FILES["pictures"]["error"][0]
$_FILES["pictures"]["error"][1]
$_FILES["pictures"]["error"][2]

true?



and then just to be sure based on real world example:
Pretend I am uploading 1 file with just 1 input in my html form...
<input type="file" name="resume" />

Is this correct how the php should look like. Bascially took the php.net multiple fileupload example and made it so it only does one since a for loop is not needed if I am doing just 1 upload (although the for loop would still work since is only doing by the array length :) ).
$key = 0;
$error = $_FILES["picture"]["error"][$key];

if ($error == UPLOAD_ERR_OK)
{
      $tmp_name = $_FILES["picture"]["tmp_name"][$key];
      $name = $_FILES["picture"]["name"][$key];
      if( !move_uploaded_file($tmp_name, "data/$name") )
      echo 'Couldn't Move File.';
}
else
echo 'Couldn't Upload File';
Link to comment
Share on other sites

cool figured it out by doing print_r()...

html for many inputs should look soemthing like this
<input type="file" name="fileupload[]" />
<input type="file" name="fileupload[]" />
And php code to upload many files can look like this…

[code]foreach ($_FILES["fileupload"]["error"] as $key => $error) //loop through errors for each file
{
if ($error == UPLOAD_ERR_OK)
{
$uploaddir = $_SERVER['DOCUMENT_ROOT'].'\\Test\\uploads\\';

$tmp_name = $_FILES["fileupload"]["tmp_name"][$key];
$name = $_FILES["fileupload"]["name"][$key];
$size = $_FILES["fileupload"]["size"][$key];
$type = $_FILES["fileupload"]["type"][$key];
if( !move_uploaded_file($tmp_name, "$uploaddir$name") )
echo 'Couldn\'t Move File.';
else
{
echo "tmp_name=$tmp_name, name=$name, type=$type, size=$size<br/>" ;
$fileUpload = "$uploaddir$name";//store for later reference maybe
}

}
else
echo 'Couldn\'t Upload File';
} [/code]

and the array looks like this:
Array (
        [name] => Array (
                [0] => test1.html
                [1] => test2.txt )
        [type] => Array (
                [0] => text/html
                [1] => text/plain )
        [tmp_name] => Array (
                [0] => C:\WINNT\TEMP\php1943.tmp
                [1] => C:\WINNT\TEMP\php1944.tmp )
        [error] => Array (
                [0] => 0 //if 1st file has errors
                [1] => 0 //if 2nd file has errors)
        [size] => Array (
                [0] => 2910
                [1] => 11199 )
        )



For 1 file upload, an array isnt needed so could can look like this... (although above multi-upload code would still work with 1 upload and should you choose to use many inputs on the page later on, might be useful to leave alone)
<input type="file" name="fileupload" />
And php code…

[code] $error = $_FILES["fileupload"]["error"]; //since only 1 file used (name is fileupload and not fileupload[] array), a 3D array isn't used, aka just use $_FILES["fileupload"]["error"] and not $_FILES["fileupload"]["error"][0]
if ($error == UPLOAD_ERR_OK)
{
$uploaddir = $_SERVER['DOCUMENT_ROOT'].'\\Test\\uploads\\';

$tmp_name = $_FILES["fileupload"]["tmp_name"];
$name = $_FILES["fileupload"]["name"];
$size = $_FILES["fileupload"]["size"];
$type = $_FILES["fileupload"]["type"];

if( !move_uploaded_file($tmp_name, "$uploaddir$name") )
echo 'Couldn\'t Move File.';
else
{//worked
echo "tmp_name=$tmp_name, name=$name, type=$type, size=$size<br/>" ;
$fileUpload = "$uploaddir$name";//store for later reference maybe
}
}
else
echo 'Couldn\'t Upload File';[/code]

and the array for 1 file upload, looks like
Array (
        [name] => test.txt
        [type] => text/plain [tmp_name] => C:\WINNT\TEMP\php1945.tmp
        [error] => 0
        [size] => 11199
        )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.