Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form Post file upload not working for large files

    Hey guys, I found the following code online, it works great for smaller files. However, when I try to use it for 32mb or so, it crashes with errors.
    Here's the form:
    Code:
       <form enctype="multipart/form-data" action="./phpscripts/testfileupload.php"
    			method="post">
    			    <input type="hidden" name="MAX_FILE_SIZE" value="1024000">
    			    <br>
    			  <input name="uploadfile" type="file">
    			    <br>
    			    <input name= "upload" type="submit" value="Upload File">
    </form>
    and here's the script:
    Code:
    ini_set('max_execution_time', 18000);
    ini_set("memory_limit","202M");
    ini_set("time_limit","5555");
    ini_set("post_max_size","2001M");
    ini_set("upload_max_filesize","2000M");
    
    
    $target_path = "../uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
    {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']).
        " has been uploaded";
    }
    else
    {
        echo "There was an error uploading the file, please try again!";
    }
    When try to upload a large file (32mb) i get the following errors.

    [16-Nov-2006 11:38:13] PHP Notice: Undefined index: uploadfile in ..../fileuploader/phpscripts/testfileupload.php on line 28
    [16-Nov-2006 11:38:13] PHP Notice: Undefined index: uploadfile in ../fileuploader/phpscripts/testfileupload.php on line 30

    How i interpret this, is that for a large file, something in the connection is immediately stopped therefore the _FILE array never gets populated. Anyone run into this before? Know any workarounds? Eventually I'm going to need a script that handles much bigger uploads. Thanks so much in advance for your expertise.

    Cheers,
    Q

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Does your webhost have any limitations on filesize?

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its set to 200M in php.ini

  • #4
    New Coder
    Join Date
    Sep 2006
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Basically the solution im looking for must let a user have an interface on a web page that lets him select a file from his local disk, and upload it to the server. I saw many examples using POST and Php, but is there a better/faster way to do this?
    I agree that my problem with my previous post could definitly be a server setup issue. What other places should i be looking for file size restrictions other than php.ini. (im currently runnnig an apache server, although i am by no means an apache expert)


    Thanks again

    Q

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    The following in your form code also sets a smaller limit than the file size you are trying to upload -
    Code:
    name="MAX_FILE_SIZE" value="1024000"
    You must always check for errors that can result from any function. Test the $_FILES['uploadedfile']['error'] element -
    Error Messages Explained
    Since PHP 4.2.0, PHP returns an appropriate error code along with the file array. The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in $_FILES['userfile']['error'].

    UPLOAD_ERR_OK
    Value: 0; There is no error, the file uploaded with success.

    UPLOAD_ERR_INI_SIZE
    Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

    UPLOAD_ERR_FORM_SIZE
    Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

    UPLOAD_ERR_PARTIAL
    Value: 3; The uploaded file was only partially uploaded.

    UPLOAD_ERR_NO_FILE
    Value: 4; No file was uploaded.

    UPLOAD_ERR_NO_TMP_DIR
    Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.

    UPLOAD_ERR_CANT_WRITE
    Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    New Coder
    Join Date
    Sep 2006
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, that makes sense. Here is the revised code.

    HTML:
    Code:
       <form enctype="multipart/form-data" action="./phpscripts/testfileupload.php"
    			method="post">
    			    <input type="hidden" name="MAX_FILE_SIZE" value="200M">
    			    <br>
    			  <input name="uploadfile" type="file">
    			    <br>
    			    <input name= "upload" type="submit" value="Upload File">
    </form>
    PHP:
    Code:
    $target_path = "../uploads/";
    //print ini_get('upload_max_filesize');
    
    print $_FILES['uploadfile']['error'];
    
    $target_path = $target_path . basename( $_FILES['uploadfile']['name']);
    
    if(move_uploaded_file($_FILES['uploadfile']['tmp_name'], $target_path))
    {
        echo "The file ".  basename( $_FILES['uploadfile']['name']).
        " has been uploaded";
    
    	print $_FILES['uploadfile']['error'];
    
    }
    else
    {
        echo "There was an error uploading the file, please try again!";
    }
    I did the error check thing, and its returning 2, which means that file is bigger than the specified limit. I changed the HTML to "200M", which is the exact same value that print ini_get(... returns. Is this not the right syntax, or is something else going on?

    Q

    Q
    Last edited by quakerstate79; 11-16-2006 at 09:58 PM.

  • #7
    New Coder
    Join Date
    Sep 2006
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok. got it working. Changed "200M", which apparently means 200 to html to "200000000" (200 million) which worked. Kinda silly imo.


    In summary, for future readers of this topic, this is the solution process i went through.

    1) I realized that even though my php.ini file had
    Code:
    post_max_size=201M;
    upload_max_filesize=200M;
    the server had not been reset since those changes were made. ALWAYS reset your server after you modify your PHP.ini file.

    2)It was brought to my attention about the error messages. ALWAYS check the errors passed in the $_FILES array
    Code:
    print $_FILES['uploadfile']['error'];
    3)Finally through step 2 i realized that my specific problem was how MAX_FILE_SIZE was set in my html. You must ALWAYS use a hidden input such as
    Code:
      <input type="hidden" name="MAX_FILE_SIZE" value="200000000">
    above the file input on your page


    Thanks again to all those that helped me. I'm giving you the best ratings possible.

    Cheers,
    Q

  • #8
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hey all

    this is interesting because i was wanting to do the same thing but where do you go to change the php.ini file???

    Im having trouble with that

    ian


  •  

    Posting Permissions

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