Jump to content

array notation used


jimfog
 Share

Recommended Posts

Recently I came across with a php script where the user is given the option of uploading files to the server.The code went like this:

<input type="file" name='fileupload[0]'...

What is the reason for the name attribute having an array notation as seen above-why someone would want to do it like this.

Link to comment
Share on other sites

If there's a variable amount of files to be uploaded, using array notation lets you loop through all of them.

foreach($_FILES['fileupload']['tmp_name'] as $index=>$location) {    move_uploaded_file($location,  $_FILES['fileupload']['name'][$index]);}

Link to comment
Share on other sites

If there's a variable amount of files to be uploaded, using array notation lets you loop through all of them.
foreach($_FILES['fileupload']['tmp_name'] as $index=>$location) {	move_uploaded_file($location,  $_FILES['fileupload']['name'][$index]);}

Well, here is the part that I have difficulty understanding-in the line:
$_FILES['fileupload']['name'][$index]

Does the above denote 3 arrays? And if yes how did we end up in having 3 arrays?

Link to comment
Share on other sites

Try to upload some (small) files, and var_dump($_FILES) to see for yourself.It's hard to explain it better than the page that birbal linked to. My best attempt would go something like this...The $_FILES array contains names (as defined in the HTML) as keys.Each value in the $_FILES array is itself a "meta data array" for the file, where the key is a certain name for a meta data about the file (name, tmp_name, size, etc.).Each value in that array is EITHER the value of that meta data OR (when there are multiple files sharing a name attribute as above) an array where the keys are whatever is specified in the HTML.If the value for the meta data is an array, the value of the array will be the meta data value for the file with this index.

Link to comment
Share on other sites

Have to study it carefully,it is not sth I can look up in just 5 min.

Link to comment
Share on other sites

Jimfrog, What I did to best understand what's going on is made a upload form with two input fields like so:

<form action="uploadtest.php" method="post" enctype="multipart/form-data">  Send these files:<br />  <input name="userfile[]" type="file" /><br />  <input name="userfile[]" type="file" /><br />  <input type="submit" value="Send files" name="submit" /></form>

Then used print_r to display and this is what the above will look for the $_FILES array:

Array (		[name] => Array		(			[0] => img.jpg			[1] => doc.txt		) 		[type] => Array		(			[0] => image/jpeg			[1] => text/plain		) 		 [tmp_name] => Array		(			[0] => C:\wamp\tmp\phpF55A.tmp			[1] => C:\wamp\tmp\phpF55B.tmp		) 		[error] => Array		(			[0] => 0			[1] => 0		) 		 [size] => Array		(			[0] => 81619			[1] => 70		) )

In the example apple, the first array index which is called 'name' will hold/container another array with the names of the uploaded files in that array's index elements. If another input field is inserted in the form, a third name will appear in index 2 of that array, etc.

Edited by Don E
Link to comment
Share on other sites

Try to upload some (small) files, and var_dump($_FILES) to see for yourself. It's hard to explain it better than the page that birbal linked to. My best attempt would go something like this... The $_FILES array contains names (as defined in the HTML) as keys.Each value in the $_FILES array is itself a "meta data array" ...
Just to be certain when you are saying "meta data array", do you mean an array within an array-i.e multidimensional array?
Link to comment
Share on other sites

Just to be certain when you are saying "meta data array", do you mean an array within an array-i.e multidimensional array?
I think I got it, the info provided at the php site is very good. It seems, that whenever multiple files are uploaded, a series of arrays are initialized. The part that confused me was that the above procedure is done automatically-except of course naming the file inputs using array notation.
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...