We help IT Professionals succeed at work.

PHP - How to increase file upload max size?

Medium Priority
625 Views
Last Modified: 2012-05-11
Hi experts, I'm trying to do a simple file upload and I can't upload any file that is greater then 2MB. I did a phpinfo() and below is what I get:

upload_max_filesize = 30M
post_max_size=40M.

I even tried with PHP:

ini_set('upload_max_filesize', '10M')
ini_set('post_max_size', '10M')
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi guys, both good info. I try uploading a file with 2+ MB on another server and it works.

--- Working Server ---
memory_limit = 128M
post_max_size = 250M
upload_max_filesize = 250MB
max_execution_time = 30
max_input_time = -1

--- NOT Working Server ---
memory_limit = 128M
post_max_size = 40M
upload_max_filesize = 30MB
max_execution_time = 30
max_input_time = 60


The thing I don't get is that both server have more then enough memory to upload around 2MB. Anyway I attached the actual php script to do the simple upload.

<?
set_time_limit(0);

/*-----------------------------------------------------------

-------------------------------------------------------------*/
function uploadImage($fileName, $prefixName="attach_file_"){
	$uploadDir="";

	//Name file by date & timestamp
	$name = date("Y-m-d-H-i-s"); 
	
	//NOTE: This allow user to name the file whatever and it will keep the file type
	$target = $uploadDir.$name.strrchr($_FILES[$fileName]['name'], '.');
	if($_FILES[$fileName]['name'] && move_uploaded_file($_FILES[$fileName]['tmp_name'], $target)){
		return true;
	}
	return false;
}
if(preg_match("/POST/i",$_SERVER['REQUEST_METHOD'])){
	uploadImage("attachFile");
}
?>
<html>
<head>

</head>
<body>
	<form action="" method="POST" enctype="multipart/form-data">
		<input type="file" name="attachFile" id="attachFile" />
		<input type="submit" value="Upload File">
	</form>
</body>
</html>

Open in new window

Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
What version of PHP?  There are bugs associated with max_input_time.  http://bugs.php.net/50180

You might change that option to match the "working" server and see if that makes any difference for you.

Author

Commented:
Thanks guys. Hernst42 comment was helpful. The error was "Failed to write file to disk". After some more time digging around. It turns out that the "upload_tmp_dir" wasn't assigned. So it uses the default directory "/tmp". I checked the linux memory and "/tmp" is set to use only 1MB. So to fix it, I assign the upload_tmp_dir to somewhere else where it allow more memory. Thanks for all the help guys.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.