Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

PHP - How to increase file upload max size?

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')
0
infernothao
Asked:
infernothao
3 Solutions
 
jeremycrussellCommented:
If its taking some time to upload, you may need to increase the max allowed script run time.
0
 
infernothaoAuthor 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

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
hernst42Commented:
Have you checked the error-code of the upload?
Add the part from http://www.php.net/manual/en/features.file-upload.errors.php#90522To see why the upload failed.
0
 
jeremycrussellCommented:
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.
0
 
infernothaoAuthor 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.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now