Jump to content


Photo

How does $_FILES array look like?


  • Please log in to reply
2 replies to this topic

#1 arianhojat

arianhojat
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 31 August 2006 - 06:50 PM

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...
...
<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" />
...

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';


#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 31 August 2006 - 07:11 PM

not sure but would print_r($_FILES); not do the trick of explaining it??

Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 arianhojat

arianhojat
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 05 September 2006 - 02:22 PM

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…

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';	
}

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…

$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';

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
        )






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users