user2443343
user2443343

Reputation: 540

$_FILES data not directly available

When I echo $_FILES['file']['name'] it gives it returns nothing When I echo $_FILES['file']['name'][0] it returns the correct data.

How come that in every example they can use $_FILES['file']['name'] directly without the extra index?

Upvotes: 1

Views: 51

Answers (1)

Ulrich Thomas Gabor
Ulrich Thomas Gabor

Reputation: 6654

How the $_FILES array is constructed is based on how your html input tags are named.

Your html input is likely

<input type="file" name="file[]" />

while in most examples it is

<input type="file" name="file" />

The first solution allows to duplicate this line exactly to receive multiple files simultaneously (in an array):

<input type="file" name="file[]" />
<input type="file" name="file[]" />

The second solution only allows exactly this single one file. If one needs to support multiple fileuploads, one has to create a html tag with a different value for the name attribute:

<input type="file" name="file1" />
<input type="file" name="file2" />

See this user comment in the php manual for a detailed example and the other comments around for futher information how to reorder this array (if one wishes to do so for whatever reason...)

Upvotes: 2

Related Questions