I have a form that lets users upload files but I'm having problems detecting files that are too large.
I have the following set in php.ini:
upload_max_filesize = 10M
post_max_size = 20M
I understand that the standard way to detect if files are larger than the config file allows is to check $_FILES['file_name']['error']. However this only works for me if the actual file is greater than upload_max_filesize and smaller than post_max_size.
If the actual file size is greater than post_max_size then the $_FILES variable is removed and I get the log message:
PHP Warning: POST Content-Length of xxx bytes exceeds the limit of
yyy ...
So the question: How do I detect if a user is uploading a file that is greater than post_max_size? I need to display a valid user message if this happens.
Thanks.
I suggest you use set_error_handler() and check for E_USER_WARNING error + appropriate message, and then send it to a user, possibly via throwing an Exception, possibly some other way.
Like this:
$filesize = $_FILES['file']['size'];
$limit = ini_get('post_max_size');
if($filesize > $limit) {
trigger_error("POST Content-Length of $filesize bytes exceeds the limit of $limit bytes.", E_WARNING);
}
else {
// upload file
}
Related
I'm trying to make file upload to server by form:
<form action="send_valid.php" method="POST" enctype= "multipart/form-data">
<br>
<input type="file" name="pdf" id="pdf" accept="application/pdf"/>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000"/>
<input type="submit" value="WyĆlij">
</form>
and I want to allow user to send only pdf files of a max size 10Mb.
My php configuration for uploads is:
file_uploads = On
upload_tmp_dir = "E:\Xampp\tmp"
upload_max_filesize = 11M
max_file_uploads = 20
post_max_size = 12M
To check file size I use:
if($_SERVER["REQUEST_METHOD"] == "POST"){
var_dump($_FILES);
if(extract($_FILES)){
if($pdf['size']>10000000){
echo "File size is too large!";
}
}
Now I want to show user an error (for now) with echo when file is too big. It works fine if it is lower than 10Mb (even the code above works when I change size to 1Mb and file is larger then it will display echo), but for files of 10Mb and above it produces that error:
Warning: POST Content-Length of 11450416 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
array(0) { }
I don't have any clue why it shows it exceeds 8Mb since in configs I couldn't find 8Mb anywhere.
Where can be the problem? Is there a way to catch an upload that exceeds configuration setting to not show user the php server error?
And if I want to make file validation does above method and checking file extension with for examle:
$ext = pathinfo($_POST['pdf'], PATHINFO_EXTENSION);
is it enough? Any insight on file validation would be really helpful.
Probably this
ini_set('post_max_size', '512M');
ini_set('upload_max_filesize', '512M');
Change 512 to any of you want.
Update the values of post_max_size and upload_max_filesize in your php configuration file.
Note that the values are measured in bytes.
Reference
http://php.net/manual/en/ini.core.php#ini.post-max-size
http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
Put this code before move_uploaded_file function
if($_FILES['pdf']['size']>10000000) {
exit("File size is too large!");
}
Thanks
I'm trying to upload image to my localhost right now. It had no problem, If it's a small image(Not over 1 MB). But when i try uploading about 3MB Image. Php said
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 18048 bytes) in C:\Users\Tharit\Desktop\Work\Pixelbar\Code\ecompro\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php on line 115
It shouldn't be this much.This is code,in image upload part.
if (Input::hasFile('profile_picture'))
{
$old_profile_pic= $user->profile_pic;
$profile_picture = Input::file('profile_picture');
$profile_picture_size=getimagesize($profile_picture);
if($profile_picture_size[0]>$profile_picture_size[1])
{
$mainside = $profile_picture_size[1];
$cordx=($profile_picture_size[0]-$profile_picture_size[1])/2;
$cordx=(int) $cordx;
$cordy=0;
}
else
{
$mainside = $profile_picture_size[0];
$cordy=($profile_picture_size[1]-$profile_picture_size[0])/2;
$cordy=(int) $cordy;
$cordx=0;
}
$filename = time() .$user->id . '.' . $profile_picture->getClientOriginalExtension();
$path = public_path('img/user/' . $filename);
Image::make($profile_picture->getRealPath())
->crop($mainside,$mainside,$cordx , $cordy)
->resize(100, 100)
->save($path);
$user->profile_pic = 'img/user/'.$filename;
if($old_profile_pic != 'img/user/default_profile.gif')
{$imagecheck=1;}
}
check your php.ini file for this line
upload_max_filesize=1M
and change it to upload_max_filesize=10M
or change "1M" to any other required file size in MBs
There are a number of parameters that you have to set in the php ini to make large file uploads possible. They are:
upload_max_filesize
post_max_size
max_input_time
max_execution_time
memory_limit
You should find helpful comments to set them in the php ini file.
Plus, its a memory error so I think it has nothing to do with file upload. memory_limit should fix it. Try setting memory_limit to -1 to assign unlimited memory and figure out if its really a memory problem.
This question already has answers here:
$_POST data returns empty when headers are > POST_MAX_SIZE
(5 answers)
Closed 8 years ago.
I have a php upload file process that works quite well. However I wanted to add some validation to ensure large files weren't uploaded, however when I was testing I noticed some strange behaviour;
I set post_max_size & upload_max_filesize to 5M (restarted server) but found that if I uploaded a file larger than 5M the POST data was lost, so I couldn't do a check on $_FILES['uploadedFile']['size']
I kinda solved the issue by ensuring that the post_max_size & upload_max_filesize were set to 10M (assuming of course no one will be silly enough to try a file bigger than that - its really for text files so they shouldn't) After which I could do my size validation without issue;
if($_FILES['uploadedFile']['size'] < 3072000)
{
//do upload stuff
}
else
{
$errorMessage .= 'File size to big, exceeds 3M Limit.';
}
I assume the problem is that the POST is getting obliterated because it exceeded post_max_size??? I searched through a lot of pages but couldn't see anyone else having a problem with this so I'm not sure if my assumption is right. If I upload a file bigger than 10Mm I still get the problem.
You are correct. Uploaded data is ignored, when the size limit is exceeded. So, you don't get access to the ['size'] info.
Instead, you need to validate your upload like this:
<?php
if ($_FILES['uploadedFile']['error'] == UPLOAD_ERR_OK)
{
// success - move uploaded file and process stuff here
}
else
{
// error
switch ($_FILES['uploadedFile']['error'])
{
case UPLOAD_ERR_INI_SIZE:
echo "The uploaded file exceeds the upload_max_filesize directive in php.ini."; break;
case UPLOAD_ERR_FORM_SIZE:
echo "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form."; break;
case UPLOAD_ERR_PARTIAL:
echo "The uploaded file was only partially uploaded. "; break;
// etc... see the full list here http://uk1.php.net/manual/en/features.file-upload.errors.php
}
}
To start off, I've set my post_max_size in /etc/php.ini to 4M just as a test. I have a check in place to check for file type and size of file. I want to only accept JPG, GIF and PNG. The file size cannot be larger than 4M. It seems that if the file size is larger than what's set in post_max_size, my code won't throw an error. If I set my PHP to deny uploads larger than let's say 2M but less than 4M, it'll throw an error (what i want). I believe this is due to PHP not accepting anything over 4M.
if (is_uploaded_file($_FILES["file"]["tmp_name"]))
{
$file_type = exif_imagetype($_FILES["file"]["tmp_name"]);
} else {
$this->form_validation->set_message('valid_image', 'Error uploading file!!!');
return false;
}
There is another ini flag upload_max_filesize that you should increase to your new limit as well
Use the value in $_FILES["file"]["error"] to check the upload status.
Not sure, but I would check for file size on the client before the upload happens. This approach would upload the whole file to the server first before giving the error even if it worked.
($_FILES['uploaded_file']['error'] == 0)
is a successful upload.
I have a file (image) upload script in PHP that I use to upload and resize images... It uses a simple MIME type and size validation so only jpg images are allowed and 1MB max file size.
I recently discovered a problem. When I try tu upload a .avi file using the script, the script processes the file like its the correct MIME type and size and then just do nothing, just takes me back to the upload form without any error message. (Instead of showing a "file too big" message).
I mean, if I try to upload a .gif or .txt or something else I get an error, as expected.
If I try to upload any file bigger than 1MB I get an error, as expected.
Only when I try to upload a .avi file with more than 1MB I dont get any kind of error.....
Well, here the first par of the code:
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 1024000);
if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'C:/Wamp/www/Version-1.4/posters_uploaded/');
// replace any spaces in original filename with underscores. At the same time, assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['image']['name']);
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'kb';
// create an array of permitted MIME types
$permitted = array('image/jpeg','image/pjpeg');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of a permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type']) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
case 0: // ...................
I'm just modifying a build PHP code so Im no expert...
Any suggestions??
Thanks.
http://us3.php.net/manual/en/features.file-upload.common-pitfalls.php
It looks like your upload_max_filesize ini-setting is too low. This would cause no error to be displayed when you upload a very large file such as an AVI video.
The reason you're seeing the errors with text files and .jpg images is likely because the size of those files are greater than 1 MB, but below your upload_max_filesize setting in php.ini.
Try echoing the value of ini_get("max_upload_filesize") and see what the value is if you don't have access to the php.ini file directly.
As john Rasch mentioned above, any file above the php.ini max_upload_filesize will not process at all. so you'll have no chance to test the error for you. you have to assume it was not uploaded and validate it if it was.
now that I understand your scenario better I think this is what you can do:
// at the top of your script
$upload_success = FALSE;
// when successfully detected upload
$upload_success = TRUE;
// if successful upload code is never run
$display_error = "File not uploaded, may be too large a file, "
. "please upload less than 1MB"
;
print $display_error;
main point being:
You can't always detect upload files that are too big because they get cut off at a level deeper than where the scripts run.
I'd also suggest that you don't believe the mime type. Sometimes people have .png or .gif file that have been renamed to .jpg, or they could upload incorrect files intentionally. Use getimagesize to check if these are valid jpeg images.
Don't forget, when uploading files, that there are actually two directives you need to pay attention to in php.ini. One is upload_max_filesize, but the other is post_max_size. Generally, post_max_size should at least be equal to, and probably greater than, upload_max_filesize. You can't upload a file greater than post_max_size, regardless of what you set your upload_max_filesize.
An AVI file won't match the mime types you have listed in your permitted array. After doing your $sizeOK and $typeOK checks, check to see what values they hold, and how your script handles those values. That might hold the key to the behavior of your script.
Above this line:
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
Put this:
echo '<pre>' . printr($_FILES) . </pre>;
That will show you what is inside the FILES array, and should make this pretty simple to debug. Try uploading the AVI with the above line added to your script.
Using $_FILES['image']['type'] for checking MIME is not reliable, it's base on header of the client and can be spoofed. Take a look at fileinfo extension to check MIME base on the real contents.
Eight years later, the short answer for anyone, like me, stumbling around for the answer to checking the image MIME type with PHP before uploading:
if (exif_imagetype($file['tmp_name']) != IMAGETYPE_JPEG) {
$file['error'] = 'Your picture must be jpg.';
}
From the manual: http://php.net/manual/en/function.exif-imagetype.php