Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-16-2006, 06:53 PM   PM User | #1
quakerstate79
New Coder

 
Join Date: Sep 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
quakerstate79 has a little shameless behaviour in the past
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
quakerstate79 is offline   Reply With Quote
Old 11-16-2006, 07:02 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Does your webhost have any limitations on filesize?
mlseim is offline   Reply With Quote
Old 11-16-2006, 07:06 PM   PM User | #3
quakerstate79
New Coder

 
Join Date: Sep 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
quakerstate79 has a little shameless behaviour in the past
its set to 200M in php.ini
quakerstate79 is offline   Reply With Quote
Old 11-16-2006, 07:15 PM   PM User | #4
quakerstate79
New Coder

 
Join Date: Sep 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
quakerstate79 has a little shameless behaviour in the past
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
quakerstate79 is offline   Reply With Quote
Old 11-16-2006, 08:09 PM   PM User | #5
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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 -
Quote:
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.
CFMaBiSmAd is offline   Reply With Quote
Old 11-16-2006, 09:50 PM   PM User | #6
quakerstate79
New Coder

 
Join Date: Sep 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
quakerstate79 has a little shameless behaviour in the past
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..
quakerstate79 is offline   Reply With Quote
Old 11-16-2006, 10:17 PM   PM User | #7
quakerstate79
New Coder

 
Join Date: Sep 2006
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
quakerstate79 has a little shameless behaviour in the past
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
quakerstate79 is offline   Reply With Quote
Old 04-19-2007, 02:40 AM   PM User | #8
ianbriggs
New to the CF scene

 
Join Date: Apr 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
ianbriggs is an unknown quantity at this point
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
ianbriggs is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:11 PM.


Advertisement
Log in to turn off these ads.