/    Sign up×
Community /Pin to ProfileBookmark

Make array of <input type=file> and detect with isset

[B]form.php[/B]

[CODE]<input type=”file” name=”image[‘pix1’]”>
<input type=”file” name=”image[‘pix2’]”>[/CODE]

[B]output.php[/B]

[code=php]if(isset($_FILES[‘image’])) {
switch($_FILES[‘image’]) {
case “pix1”: echo “one”;
case “pix2”: echo “two”;
break;
}
}[/code]

The above code won’t work, but it illustrates what I want to achieve. Does anyone know how to do this?

Thanks!

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 12.2005 — The above code won't work, but it illustrates what I want to achieve. [/QUOTE]Maybe to a psychic! Anyway the following, for example, should print the file names:

[B]form.php[/B][code=php]<input type="file" name="image[]">
<input type="file" name="image[]">[/code]


[B]output.php[/B][code=php]if(!empty($_FILES['image']['name'])){
foreach($_FILES['image']['name'] as $k => $v){
print '$_FILES["image"]["name"]["'.$k.'"] = '.$v;
}
}[/code]


Don't forget that the form tag needs to contain [B]enctype[/B]
Copy linkTweet thisAlerts:
@NogDogNov 12.2005 — You must include the enctype as below, and should include the max size:
[code=html]
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="your_page.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
<!-- Name of input element determines name in $_FILES array -->
<input name="userfile" type="file">
<input type="file" name="image['pix1']">
<input type="file" name="image['pix2']">
</form>
[/code]

[code=php]
if(isset($_FILES['image']))
{
foreach($_FILES['image'] as $key => $value)
{
echo "<p>Found $key</p>n";
}
}
[/code]

If that does not work, try changing the input names both to just "image[]" and let PHP generate the array indices.
Copy linkTweet thisAlerts:
@bokehNov 12.2005 — [code=html]<input type="file" name="image['pix1']">
<input type="file" name="image['pix2']">[/code]
[/QUOTE]
If you want to specify the keys as shown here do not use the quotes as they will come through escaped. Instead just use [B]name="image[pix2]"[/B]. Also, I think this:[code=html]<input name="userfile" type="file">[/code][/QUOTE] is in there by accident. I'm not sure though.

Also sometimes it is easier to understand the files array if you print it out... so here it is... without assigning keys:[code=php]Array
(
[image] => Array
(
[name] => Array
(
[0] =>
[1] =>
)

[type] => Array
(
[0] =>
[1] =>
)

[tmp_name] => Array
(
[0] =>
[1] =>
)

[error] => Array
(
[0] => 4
[1] => 4
)

[size] => Array
(
[0] => 0
[1] => 0
)

)

)[/code]
Copy linkTweet thisAlerts:
@magoauthorNov 13.2005 — Ok, I've come this far:

input.php[code=html]<input type="file" name="image[pix1]">
<input type="file" name="image[pix2]">[/code]

output.php[code=php]if(isset($_FILES['image']['name'])) {
foreach($_FILES['image']['name'] as $key => $value) {
if($value){
echo "<p>Found $key $value</p>n";
}
}
}[/code]

That works fine, but I now need to echo the tmp_name for each $_FILES, so I tried this...[code=php]echo $_FILES['image'][$key]['tmp_name'];[/code]...but it doesn't work.

Any suggestions?

.
Copy linkTweet thisAlerts:
@SpectreReturnsNov 13.2005 — Riiight ? Try something like this:

input.php
[code=html]<input type="file" name="image[]">
<input type="file" name="image[]">[/code]


output.php
[code=php]foreach($_FILES['image'] as $key => $img) {
echo $img['tmp_name']."n";
}[/code]
Copy linkTweet thisAlerts:
@bokehNov 13.2005 — You have your order wrong. Thats why I posted the $_FILES array above.

[code=php]if(isset($_FILES['image']['tmp_name'])) {
foreach($_FILES['image']['tmp_name'] as $key => $value) {
if($value){
echo "<p>Found $key => $value</p>n";
}
}
}[/code]
Copy linkTweet thisAlerts:
@bokehNov 13.2005 — echo $img['tmp_name']."n";}[/code][/QUOTE]That's wrong in this case because since it is a multiple upload ['tmp_name'] contains an array. Check out the array structure above.

Mago, Can you explain properly what your script is supposed to achieve?
Copy linkTweet thisAlerts:
@magoauthorNov 13.2005 — Here is how to get the tmp_name:

[code=php]$_FILES[image]['tmp_name'][$key];[/code]Thank you all for your help.

Regards
Copy linkTweet thisAlerts:
@magoauthorNov 23.2006 — 
input.php[code=html]<input type="file" name="image[pix1]">
<input type="file" name="image[pix2]">[/code]

output.php[code=php]if(isset($_FILES['image']['name'])) {
foreach($_FILES['image']['name'] as $key => $value) {
if($value){
echo "<p>Found $key $value</p>n";
}
}
}[/code]

[/QUOTE]

Assuming that I have 10 <input type="file">, would it be possible to set a maximum of 5 files per upload? Basically, I am looking for something like this:
[code=php]if(isset($_FILES['image']['name']) AND <6 files in total) {
foreach($_FILES['image']['name'] as $key => $value) {
if($value){
echo "<p>Found $key $value</p>n";
}
}
}[/code]

With kind regards,
Copy linkTweet thisAlerts:
@bokehNov 23.2006 — [code=php]if(isset($_FILES['image']['error']) &&
is_array($_FILES['image']['error']) &&
($keys = array_keys($_FILES['image']['error'], 0)) &&
count($keys)<6)
{
foreach($keys as $key)
{
echo "key: $key, filename: {$_FILES['image']['name'][$key]}<br>n";
}
}[/code]
Copy linkTweet thisAlerts:
@magoauthorFeb 05.2007 — Bokeh, thanks for your reply.

It seems that your code is counting ALL <input type="file">-tags and not only those which have been set. I have 10 <input type="file"> and for the code to work I have to set count() to <11.

Normally, isset() should take care of this, so I don't understand why it's not working...

Here is the code again:
[code=php]if(isset($_FILES['image']['name']) && is_array($_FILES['image']['name']) && ($keys = array_keys($_FILES['image']['name'], 0)) && count($keys)<6) {
echo "OK";}
else {die("max 5 files per upload");}[/code]


Best regards,
Copy linkTweet thisAlerts:
@NightShift58Feb 05.2007 — I think that there's a misunderstanding here.

Mago, by the time your script is able to test the $_FILES array, the files have already been uploaded into PHP's temporary upload directory.

At that point, all you can do is move the 5 files that you will accept to their final destination and delete the ones you will not accept - or just leave and they will eventually be deleted - I believe.

If the form has 10 <input type=file>, you cannot stop the user from uploading 10 files. You can refuse to process them, yes, but that's all.
×

Success!

Help @mago spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...