Invalid argument supplied for foreach() in upload.php

I downloaded a copy of the code you guys have on here to do drag & drop files with PHP and JavaScript. Inside my own network, everything works fine. I was trying to have a family member upload some files onto my server using this code, but we are getting the following error…

PHP Warning: Invalid argument supplied for foreach() in /usr/lib/cgi-bin/upload.php on line 24

I’m not familiar at all with PHP…I mostly script with CGI/Perl, but have had a problem doing this task with those. I’ve tried doing single and multiple files from within my network and outside. Inside works fine…outside not so much. It gets about 200k or so of the 2MB file. So I’m not sure if my web server is dropping the connection or what.

One thing that did come to mind…could there be a problem with running over a secure connection instead of unsecure? Thanks.

Other than that I think the code is AWESOME…it works really nicely. I need to add in the progress bar though.

Hi ryan.prather, welcome to the forums.

I don’t know what code you’re referring to, but the “invalid” most likely means the variable you are using is not an array. Easy would be you typoed it. Harder would be you need to follow the code back to see where things are going wrong.

This is the code block in the PHP file that is giving the error.


// form submit
$files = $_FILES['fileselect'];

foreach ($files['error'] as $id => $err)
{
	if ($err == UPLOAD_ERR_OK)
	{
		$fn = $files['name'][$id];
		move_uploaded_file(
        		$files['tmp_name'][$id],
			'uploads/' . $fn
		);
		echo "<p>File $fn uploaded.</p>";
	}
}

I retrieved it from “http://www.sitepoint.com/html5-ajax-file-upload/”. I copied the code directly and did very few changes except for the “{” placements.

Try adding this


var_dump($_FILES); // for debugging only
// form submit
$files = $_FILES['fileselect'];
foreach ($files['error'] as $id => $err)
{
	if ($err == UPLOAD_ERR_OK)
	{
		$fn = $files['name'][$id];
		move_uploaded_file(
        		$files['tmp_name'][$id],
			'uploads/' . $fn
		);
		echo "<p>File $fn uploaded.</p>";
	}
}

You should see whether or not there is an index of “fileselect” with an array with an index of “error”

I tried adding that, but I don’t know how to see the output. If I run “php5 uploads.php” then it generates “array(0){}”. But the script only get’s called when it submits the AJAX with the file info. And as I said, everything works just fine on my intranet. The problem only seems to occur when I open it to the internet and try to access it from outside.

Are you using FireBug? Open the Network panel and fire off an Ajax request - the request will show in FireBug. You can right click it and copy it with url parameters, then paste that in to the browser. Then paste this in to a new tab so you can see the ajax content easily in your browser window (including the var_dump’d stuff). Hopefully then you can provide that output to help debug further…

That doesn’t really work either, because it’s a script to upload an image. So I can’t really do a direct AJAX call.

Did you try a lower weight jpg? Or change the attr val

<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />

292K is close to “about 200k or so”