Jump to content


Photo

invalid argument warning


  • Please log in to reply
13 replies to this topic

#1 bmbc

bmbc
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 16 April 2006 - 01:52 AM

<?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");
   }
}
?>

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

Thanks

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 02:24 AM

It's the line before. You forgot the quotes on the ini_set. Example:

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



#3 bmbc

bmbc
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 16 April 2006 - 02:41 AM

Thanks but I made the change and I'm still getting the same warning.

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 02:46 AM

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]



#5 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 16 April 2006 - 02:47 AM

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.
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#6 bmbc

bmbc
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 16 April 2006 - 02:51 AM

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?

#7 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 03:03 AM

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]



#8 bmbc

bmbc
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 16 April 2006 - 03:14 AM

<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>

What is wrong with this?

#9 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 03:17 AM

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


#10 bmbc

bmbc
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 16 April 2006 - 03:21 AM

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 :)

#11 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 03:34 AM

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.

#12 bmbc

bmbc
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 16 April 2006 - 03:47 AM

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.

#13 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 04:05 AM

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.



#14 bmbc

bmbc
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 16 April 2006 - 04:19 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users