Click to See Complete Forum and Search --> : Invalid argument supplied for foreach()


$var
11-06-2006, 12:07 PM
Hi, I'm using the following code in a multiple file upload,
it used to work, but i moved servers and now i get an error in the foreach area:

Warning: Invalid argument supplied for foreach()
//START IMAGE UPLOAD
$path = $_SERVER['DOCUMENT_ROOT']."/it/files/".$issueID;
if(!is_dir($path)) { //if $path isn't a dir
mkdir($path, 0755); //create it
}
foreach( $HTTP_POST_FILES as $aFile )
{
copy ($aFile['tmp_name'], $path."/".$aFile['name'])
or die ("");
}

Any ideas? The form is multi-part, in case that was a thought.

The Little Guy
11-06-2006, 12:40 PM
Here is how PHP.net says to do it:

<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>

<?php
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, "data/$name");
}
}
?>

NogDog
11-06-2006, 12:41 PM
Possibly you need to use $_FILES instead of $HTTP_POST_FILES. (It might be a scoping issue, such as if the code is within a function definition, and $_FILES is a "super global" so should always be in scope.)

$var
11-06-2006, 12:49 PM
it was the $HTTP_POST_FILES that threw the error.
thanks!