Learn how to a build a cloud-first strategyRegister Now

  • 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

I even tried with PHP:

ini_set('upload_max_filesize', '10M')
ini_set('post_max_size', '10M')
3 Solutions
If its taking some time to upload, you may need to increase the max allowed script run time.
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.



function uploadImage($fileName, $prefixName="attach_file_"){

	//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;

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

Open in new window

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.

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

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