Jump to content

Archived

This topic is now archived and is closed to further replies.

bmbc

invalid argument warning

Recommended Posts

[code]<?php
// ini_set() does not work under safe mode
ini_set(upload_max_filesize, "50kb");
foreach ($_FILES["pictures"]["error"] as $key => $error) {
   if ($error == UPLOAD_ERR_OK) {
       $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
       $name = $_FILES["pictures"]["name"][$key];
       move_uploaded_file($tmp_name, "upload/$name");
   }
}
?> [/code]

I don't know why there is a warning on the line foreach.
Any help appreciated.

Thanks

Share this post


Link to post
Share on other sites
It's the line before. You forgot the quotes on the ini_set. Example:

ini_set('upload_max_filesize', '50kb');

Share this post


Link to post
Share on other sites
Thanks but I made the change and I'm still getting the same warning.

Share this post


Link to post
Share on other sites
Then it means that $_FILES["pictures"]["error"] is not an array.

You can only pass an associative array to the foreach.


[a href=\"http://us2.php.net/manual/en/features.file-upload.php\" target=\"_blank\"]http://us2.php.net/manual/en/features.file-upload.php[/a]

Share this post


Link to post
Share on other sites
an "Invalid argument in foreach" error is thrown when you give foreach() something that isn't an array. So you need to make sure that $_FILES['pictures']['errors'] is indeed an array.

Share this post


Link to post
Share on other sites
Thanks again but sorry how do I do that?
pictures is in the htm - is that what you mean?
I checked the php on a site called www.meandeviation.com/tutorials/ learnphp/php-syntax-check/
and it didn't come up with any syntax errors.

Ok I just went to that link you gave me and there is
the script I'm using which comes up with the exact
same warning. Why are they giving that example if it
doesn't work?

Share this post


Link to post
Share on other sites
HTML form must be an array (use of []). See code example at:

[a href=\"http://us2.php.net/manual/en/features.file-upload.php#AEN7125\" target=\"_blank\"]http://us2.php.net/manual/en/features.file...oad.php#AEN7125[/a]

Share this post


Link to post
Share on other sites
[code]<div id="upload">
<form action="" method="post" enctype="multipart/form-data">

<p>Pictures:
<li><input type="file" name="pictures[]" /></li>
<li><input type="file" name="pictures[]" /></li>
<li><input type="file" name="pictures[]" /></li>
<li><input type="file" name="pictures[]" /></li>
<li><input type="file" name="pictures[]" /></li>
<li><input type="submit" value="Send" /></li>
</p>
</form>
</div> [/code]

What is wrong with this?

Share this post


Link to post
Share on other sites
[!--quoteo(post=365217:date=Apr 15 2006, 08:14 PM:name=bmbc)--][div class=\'quotetop\']QUOTE(bmbc @ Apr 15 2006, 08:14 PM) [snapback]365217[/snapback][/div][div class=\'quotemain\'][!--quotec--]
What is wrong with this?
[/quote]
Action is missing it's value.

Share this post


Link to post
Share on other sites
I'm really sorry but I just don't follow.
action is missing it's value- which action(?) and
why does the php tutorial use the exact same
code as the working example?

OOOOOPS SORRY!!!!! I just got it - how embarrassing is that!
Ok so the action should be upload right or should it be post?
Anyway thanks :)

Share this post


Link to post
Share on other sites
Action contains the name of your PHP script that's going to handle the upload processing (like the name of the script that contains the code in your original post here). Example:

<form action="myuploadscriptname.php" method="post" enctype="multipart/form-data">

So, when the form is submitted it will send all the $_FILES data filled-in to your upload.php script.

Share this post


Link to post
Share on other sites
Yes, I just worked that out :)
I separated the php page from the htm one and set the action to the name of the php
and there is no more warning.
Problem now is the file size restriction doesn't work , it just ignores it and
let's me upload any size I want.
Also I need to create an error and successful upload page script.
Plus I was hoping to be able to send text with the images to the same uploads folder
but I don't know how that would work let alone so the images and text would be
associated.

Share this post


Link to post
Share on other sites
For PHP <= 4.2.3 you can set it using ini_set(). But if you have a higher version PHP then upload_max_filesize can only be set in the php.ini file or .htaccess file.

[a href=\"http://us2.php.net/manual/en/ini.core.php#AEN246248\" target=\"_blank\"]http://us2.php.net/manual/en/ini.core.php#AEN246248[/a]

Read some more tutorials.

Share this post


Link to post
Share on other sites
Thanks so much for your help today toplay.
I will save any other questions I have for another time when I've read more of the
tutorials and forum entries.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.