...

View Full Version : Form Post file upload not working for large files



quakerstate79
11-16-2006, 07:53 PM
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:

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

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

mlseim
11-16-2006, 08:02 PM
Does your webhost have any limitations on filesize?

quakerstate79
11-16-2006, 08:06 PM
its set to 200M in php.ini

quakerstate79
11-16-2006, 08:15 PM
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

CFMaBiSmAd
11-16-2006, 09:09 PM
The following in your form code also sets a smaller limit than the file size you are trying to upload -

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.

quakerstate79
11-16-2006, 10:50 PM
ok, that makes sense. Here is the revised code.

HTML:

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

$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

quakerstate79
11-16-2006, 11:17 PM
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

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

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

<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

ianbriggs
04-19-2007, 03:40 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum