I'm working on creating a photo upload form and I'm running into some trouble. Essentially, a user fills out some basic demographic data, checks a media release, selects a photo, and uploads it. I then use a few nested if statements to validate that it's the correct photo size, type, etc.
At times it works just fine, but with certain photos I've been getting this error:
PHP Warning: POST Content-Length of 11310075 bytes exceeds the limit
of 8388608 bytes
Followed by a bunch of
PHP: Notice: Undefined Index
for each of elements in my $_POST array. I did some digging with phpinfo() and found that memory_limit is set to 128M...so I'm confused as to what's going wrong.
I'm using MODX, Apache/2.2.25
Thanks for your help!
The issue is not about memory, but max upload/post data limit. Please check you phpinfo() for:
post_max_size
upload_max_filesize
These values should be increased. This can be done by editing php.ini file, or by set_ini() function.
You can set unlimited memory usage with this code.
ini_set('memory_limit', '-1');
Related
I'm using XAMPP for my project. I'm trying to upload really big images and I've noticed that it doesn't work with all images.
After trying it out a few times I came to the conclusion that images which have a higher resolution than something about 6500px in width do not upload.
I've also found out that the file size doesn't seem to matter since a 1.4MB Image with a resolution more than 6500px won't upload but another with 4.8MB but small in resolution uploads without any problem.
Somehow the reason why the image is not being uploaded is with the resolution and not with the file size.
The only code I've to show for is the upload. However there's nothing special about it. As mentioned, other images upload perfectly fine, only the ones with a too high resolution don't.
php code:
move_uploaded_file($imageUploadFile, $taget_original)
php.ini
post_max_size=10000M
upload_max_filesize=10000M
Is there any solution to this problem? Do I need to specify somewhere that I want to upload high resolution images?
This is really important since I want to be able to upload 8k to 16k images. At the moment this doesn't work even if the file size should be small enough, it won't upload the image for some reason.
I wouldn't be looking in the upload size department but in the (allowed) memory size department (e.g. memory_limit. I bet you're using ImageMagick or something to actually do something with the image.
Also see here and here. Just make sure you read the documentation because the values are supposed to be specified in bytes, not megabytes (also see the comments on those answers).
I would try something like:
$limit = 2 * (1024 * 1024 * 1024); // 2Gb
// set memory limit
ini_set(‘memory_limit’, $limit); // For testing purposes you could try -1 (for unlimited) instead of $limit
// pixel cache max size
IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, $limit);
// maximum amount of memory map to allocate for the pixel cache
IMagick::setResourceLimit(imagick::RESOURCETYPE_MAP, $limit);
What the actual limit is supposed to be I guess will have to be found out by trial-and-error and will also depend on the amount of memory available ofcourse. If you're on shared hosting then this might (or: most likely will) be a problem.
I had a similar case in the future. Quite a strange solution, but it worked for me.
Try to specify the size with MB, not M
upload_max_filesize = 256MB
post_max_size = 256MB
It should work. If not, try to increase memory_limit
I hope it helps
Some Updates:
I've looked into my javascript programming a little and found a few interesting non working implementations.
It seems that this was all a client side problem.. Or at least I think it is. For Some reason my onprogress function doesn't work correctly. I've tried to upload Images with a bigger delay and sometimes this worked out.. other times it didn't though.
I'm not really sure if the client side problem is causing all of this. I'll probably just have to fix the front-end issue and hope the backed issue resolves itself.
Either way I'm going to update this question as soon as I've tried to fix everything.
There are several places where this can fail:
the size of the POST allowed by the webserver (it base 64 encoded hence larger than the file size)
the time limit allowed by the webserver for a client to make a request
the max upload size allowed by PHP
the memory available in PHP to load and process the image (assuming you do anything other than move_uplaoded_file()
Except for the last of these, it has nothing to do with the dimensions of the image.
I created an API for data in a website.
This API creates a php file, which is put in a cache directory.
(Data are stored in an array and json_decode)
It works until my file weighs less than 5-6 Mo, after this size i have a white page.
Several possibilities come to mind
Php limit size : i try to insert ini_set('memory_limit', '1024M'); but no
JSON limit size
Server limit size
I know my question is very "open" but any help will be appreciate
During the resize of an image I receive the error "Out of Memory" of PHP. I can solve it by set a greater value in my php.ini but what happens if I can't modify my php.ini (and also I can't set this value runtime with PHP) due webhosting security policy?
I upload the image using a normal post with $_Files, my memory_limit is 32 mb. How can I calculate if an image will cause this error during the resize? The uploaded photos may have different formats and weight; I’m trying to resize to an unique width of 820px.
EDIT
I have found this site http://www.dotsamazing.com/en/labs/phpmemorylimit
Maybe i have to replicate this calculation but it seems very hard to do.
It looks like you would want to get the image size before you attempted to resize it.
For example once you have a temporary path for the file on the server, pass it to the function:
$image_size = getimagesize('/path/to/image');
This will provide you will lots of info about the image, including it's size.
Once you have tthis, then subtract your memory_limit, which you can get the value of with:
$my_memory_limit = ini_get('memory_limit');
So your available memory becomes
$memory_available = $my_memory_limit - $size;
At which point you can make the decision of whether you have the memory to resize it (depending on how memory costly your re size process is, or if you kick it back to the user and tell them to use a smaller image.
I'm getting some variables with $_POST, mainly inputted by the user on a form.
To prevent huge values to be inserted in the form, I can check those variables and return an error to the user if they're to long.
However, I can't do that if the user input a REALLY long value because, in this case, the entire script crash before I can even refuse to process that variable. Is this a threat to the security of my server?
How can I fix this without increasing the default memory limit? Is it possible?
Restraining user input with a javascript could be an option, but the user may easily overcome that limit by disabling javascript.
You can limit the maximum size of a post body using the ini directive post_max_size. However its defaults to 8M. If it is unchanged it should be ok as the default memory_limit is 128M. (php 5.3)
But of course this depends on what you are about to do with the data and how much memory PHP will consume for this. In extreme speak, it would be even possible to create an example script that reaches the memory limit with a 1 byte input. So you'll have to find the proper value for your application.
From the documentation
Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. If memory limit is enabled by your configure script, memory_limit also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size. When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. , and then checking if $_GET['processed'] is set.
Do you have access to php.ini? You can set upload_max_filesize / post_max_size
See here for details: http://php.net/manual/en/ini.core.php#ini.sect.file-uploads
(p.s. setting maxlength in the HTML form is easily circumvented by a malicious user)
Divides the data on the client into parts and download in parts via Ajax, then save the downloaded part on the disc or in the database. Maybe this way?
I have a PHP form for uploading files and it works fine and displays an error message if something went wrong. This is all good.
The problem is when I test with a really big file, it just refreshes the page as if I hadn't sent a file at all, and none of the $_POST variables from the form are even sent to the server.
I want to display an error to the user, letting them know that the file is too big. However, I can't do this.
Does anyone know what is happening?
Check your PHP ini file, which governs how large a file PHP will allow to be uploaded. These variables are important:
max upload filesize (upload_max_filesize)
max post data size (post_max_size)
memory limit (memory_limit)
Any uploads outside these bounds will be ignored or errored-out, depending on your settings.
This section in the docs has the best summary: http://ca3.php.net/manual/en/features.file-upload.common-pitfalls.php
EDIT: Also note that most browsers won't send uploads over 2GB in size. This link is outdated, but gives an idea: http://www.motobit.com/help/scptutl/pa98.htm. Anyone have a better source of info on this?
There are also limits that can be imposed by the server, such as Apache: http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestbody
To truly see what's going on, you should probably check your webserver logs, check the browser upload limit (if you're using firefox), and try seeing if print_r($_FILES) generates any useful error numbers. If all else fails, try the net traffic monitor in firebug. The key is to determine if the request is even going to the server, and if it is what the request (including headers) looks like. Once you've gotten that far in the chain, then you can go back and see how PHP is handling the upload.
Your $_POST is most likely empty because the upload exceeded the post_max_size directive:
From PHP's directives page:
If the size of post data is greater
than post_max_size, the $_POST
and $_FILES superglobals are empty.
This can be tracked in various ways,
e.g. by passing the $_GET variable to
the script processing the data, i.e.
<form
action="edit.php?processed=1">, and
then checking if $_GET['processed']
is set.
you can not know the size of the file that is being uploaded, until it gets fully uploaded. so if you want to determine the file size, and then show an error for large files, first you must have the file uploaded, then check the file size. inorder to have a large file uploaded, you should set 2 settings in your php configuration file, php.ini.
open it up and search for "upload_max_filesize" and set the value to maximum value you want. then you must set the maximum value for POST parameters, because uploaded files are recieved via HTTP POST method. find "post_max_size" and set it to a large value to.
apart form the options as the former users answered:
max upload filesize (upload_max_filesize)
max post data size (post_max_size)
memory limit (memory_limit)
there is also one, when the file is very large, or the line is busy, there need to much time to execute this operation, which will hit to ceil of the script execution limit.
max_execution_time
I would implement a simple script that does regular ajax calls to know how much of the upload has been done. You can then implement some sort of progress bar. There are a couple of examples on the net:
http://martinjansen.com/2007/04/28/file-upload-progress-bars-with-php/
max upload filesize (upload_max_filesize)
max post data size (post_max_size)
are the directives you need to set. I have tested it with multiple OS, browsers etc and these are the only two thing you need be worried about.