SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    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.

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the code block in the PHP file that is giving the error.

    Code:
    // 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.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Try adding this
    Code PHP:
    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"

  5. #5
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

  7. #7
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Did you try a lower weight jpg? Or change the attr val
    HTML Code:
    <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />
    292K is close to "about 200k or so"


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •