/    Sign up×
Community /Pin to ProfileBookmark

foreach loop fails – help

hello,

i am trying to build multiple file upload form…
where the number of files is unknown… (but maximum 5)

the first thing i want to check beofre moving files to server is to check its size,

and i try to get size like this:

[code=php]
foreach($_FILES[$fieldname][‘size’] as $KEY => $filesize)
{

if($filesize < $allowedsize
{
next;
}
else
{ stop and print error}

}
[/code]

but i come to error when file size is more then upload_max_filesize or post_max_size

post returns an empy string and foreach fails saying:

[B]Warning: Invalid argument supplied for foreach() in C:Program Files.[/B]

help please…

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@TinKimSep 09.2009 — The warning says that there it is not an array that you are trying to walk through with foreach. You should ensure that you have the right datatype being used in your vars or the superglobals.

In this case, you can simply debug by using print_r on $_FILES. You should first take a look at what you have really in there.

--

www.youngvisions.net (german) | www.schneider.ws (coming soon)
Copy linkTweet thisAlerts:
@ryanlundSep 09.2009 — Ok,

$_FILES[$fieldname]['size'] is not an array, its a part of an array...therefore you cant run foreach on it.
Copy linkTweet thisAlerts:
@SodbusterSep 09.2009 — $_FILES[$fieldname]['size'] is not an array...[/QUOTE]$_FILES[$fieldname]['size'] [B]is[/B] an array.

As the manual says: "...$_FILES['userfile']['size']...will be a numerically indexed array of the appropriate values for the submitted files."
Copy linkTweet thisAlerts:
@ryanlundSep 09.2009 — Fair play ....
Copy linkTweet thisAlerts:
@NogDogSep 09.2009 — $_FILES[$fieldname]['size'] [B]is[/B] an array.

As the manual says: "...$_FILES['userfile']['size']...will be a numerically indexed array of the appropriate values for the submitted files."[/QUOTE]


Only if you used the array-style naming for the file input field, which at this point we do not know for sure.
<i>
</i>&lt;input type='file' name='userfile[b][color=red][][/color][/b]' /&gt;

Otherwise it will be a scalar value.
Copy linkTweet thisAlerts:
@SodbusterSep 09.2009 — That's correct. I should have said "$_FILES[$fieldname]['size'] [B]should be[/B] an array for a multi_file upload."
×

Success!

Help @LogicOpinion 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,
)...