Symfony2 - maxSize parameter not working on file upload - php

I have a file uploading function on my Symfony2 project.
I am seting the maxSize parameter like that:
$manuscript_file = new File(array(
'maxSize' => '20M',
'mimeTypes' => array(
'application/msword',
'application/zip',
),
'mimeTypesMessage' => 'Please upload a valid manuscript file. Valid types are: doc, docx, zip',
));
The problem is that when I am trying to upload a 2M or 3M word file, I am getting the validation message:
The file is too large. Allowed maximum size is 20M bytes.
Did you faced that? Or is my code wrong.
I took the example from the Symfony documentation:
Symfony File - Validation Constraints Reference

I already faces this issue, so I post this solution (I think this is the same issue for you).
This is a known bug of Symfony, in fact the framework will display the validator error message also when the file size is too high for your PHP configuration, instead of getting the classical PHP error.
In your current PHP config, you probably limited the max upload size to 2M, so Symfony display the wrong error.
So check your php.ini file (/etc/php5/apache2/php.ini on Linux) and increase max_upload_size to fit your field :
upload_max_filesize = 20M
Don't forget to restart apache : apache2ctl restart
Now it should work !
Note that's probably fixed on the last Symfony version, another solution is perhaps to upgrade your project to sf2.3 (but i'm not sure of that) ^^

I created a jQuery validation method to prevent sending big files to server because php don't valide it (It is in Spanish):
$(function() {
//Validate 20MB
validarFileSize('#carga_telefonos_form_file', {{ 10*1024*1024 }}, '#div-mensaje-file-size', '#botonSubmit');
});
function validarFileSize(campo, maximo, divMensaje, btnGuardar) {
console.debug("validarFileSize. Campo: " + campo + ", maximo: " + maximo);
$(campo).bind('change', function() {
var size = this.files[0].size;
if (size > maximo) {
$(divMensaje).show();
$(btnGuardar).attr('disabled', 'disabled');
} else {
$(divMensaje).hide();
$(btnGuardar).removeAttr('disabled');
}
});
}

If you are using POST to upload your file, beware with post_max_size limit in php.ini

Related

Laravel file upload validation doesn't trigger - TokenMismatchException

I can upload the files if they are fitting this validation rule
'user_file' => 'file|max:10240|mimes:xls,xlsx,doc,docx,pdf,zip'
all goes fine.
I have set my upload_max_filesize to 32MB and post_max_size to 40MB in php.ini
but if i try to upload a file bigger than 40MB my validation rules don't even trigger. I get TokenMismatchException error....
If someone can verify this by simply trying to upload some very big file (a video file for example)
When You exceed post payload size - everything is dropped, so csrf_token does not come to laravel and the upload file is empty so it cannot be validated.
UPDATE
To fix this you need to check the file size before the uploading with javascript or jquery
here is an example:
How to check file input size with jQuery?
In the case of file uploads, the file has to be copied to temp location into the server then the rules will work. so your server will not allow files of size greater than 40MB (post_max_size) into the temp location so rules will not work.
Instead, to fix this you need to do frontend validation for files.
You can do this using simple Javascript as shown below,
$('input[type="file"]').change(function () {
if (this.files[0] != undefined) {
var name = this.name;
var filesize = this.files[0].size;
var field = $('#' + this.name).parents('.input-group');
//check if file size is larger than 3MB(which is 3e+6 bytes)
if (filesize > 3000000) {
alert(filesize);
//reset that input field if its file size is more than 3MB
$('[name="' + name + '"]').val('')
}
}
});
you can just include this for all input of type='file' by changing size limit in bytes.
I encountered the same problem. You should as well check that the validator is checking file data, not post data :
I've tested :
$validator = Validator::make($request->post(), [
myfield' => 'required|image|mimes:jpeg,png,jpg,gif|max:1000000'
]);
Should have been :
$validator = Validator::make($request->file(), [
myfield' => 'required|image|mimes:jpeg,png,jpg,gif|max:1000000'
]);

PHP, Dropzone Laravel 5.1 422 (Unprocessable Entity)

I really hope someone can help me with this issue as I've tried everything I know.
The Issue:
Dropzone doesn't upload any images above 3mb instead shows 422 (Unprocessable Entity), images bellow 3mb upload perfectly fine. I've tried everything possible as well as spent plenty of time searching Google, I am receiving the issue both on local machine (Mac OSX using MAMP pro) and on my linux server (ubuntu 14.0). I believe this may be either a laravel or dropzone issue that I cant seem to figure out.
The File I'm trying to upload is straight from a cannon cam, 8mb filenames date+time.JPG, Ive checking the files via saving them as different outputs .jpg, .jpeg, .png however it still fails, they do work if I save them for web and optimize bellow 3mb however I need to be able to upload at least 9mb.
PHP Ini Settings:
upload_max_filesize = 30M
post_max_size = 30M
Form Settings:
Standard laravel open form with crftoken (_token)
DropZone Settings:
Dropzone.options.templateDrop = {
maxFilesize: 30,
maxThumbnailFilesize:15,
acceptedFiles: ".jpeg,.jpg,.png,.gif",
init: function () {
this.on("addedfile", function (file) {
//Show loader whilst uploading
$('.jqueryLoader').show();
});
this.on("complete", function (file) {
//when images are fully uploaded reset div and functions within
if (this.getUploadingFiles().length == 0 && this.getQueuedFiles().length == 0) {
$('#galleryImageHolder').load(document.URL + ' #galleryImageHolder', function(){
galleryFunctions();
$('.jqueryLoader').hide();
});
}
});
}
};
Thanks in advance for any help you can offer.
Kind Regards,
Martyn
Are you doing any server-side validation in Laravel? For example, my Upload Request sets a maximum file size on an upload:
public function rules()
{
$rules = [
'file' => 'max:2048'
];
return $rules;
}

Request Entity Too Large with small files

I know there are many questions about Request Entity Too Large on internet but i could not find right answer for my problem ;)
I`m using HTML file input tag to let users upload their images .
<input type = 'file' class = 'upload-pic' accept='image/*' id ='Fuploader' name = 'pro-pic'><br>
There is nothing wrong with files less than 2 MB which is allowed by my site
But the problem is if some one decide to upload larger file like 5.10 MB , i can handle it by php and warn user that this file is too large
if($_FILES['pro-pic']['size'] > 2000000)
{
die("TOO LARGE");
}
But my problem is by uploading 5.10 MB file , Request entity too large error will be lunched and rest of my php code won`t work
I have checked post_max_size and upload_max_filesize they are both set to 8MB
But i get Error on 5.10MB !
And I need to find way to handle files even larger than 8MB because there is no way to guess what user may try to upload ;) and i don`t want them to get dirty and broken page because of REQUEST ENTITY TOO LARGE ERROR
Is there any way too fully disable this Error Or set upload_max_filesize and post_max_size to infinity ?
You need to set SecRequestBodyAccess Off.
Check the link i have given ..it would help you
https://serverfault.com/questions/402630/http-error-413-request-entity-too-large

Codeigniter : mp4 video won't upload, no error

I'm creating a form to upload video file, but I have a strange error.
My form is like this:
<div style="color : red;"><?php echo $error;?></div>
<?php echo form_open_multipart('data_center/ajout_ressource/formulaire_video'); ?>
//various input text fields...
<input type="file" name="video">
<input type="submit" value="Ajouter cette ressource" />
</form>±
My method to get upload is (it's inside something to check if the rest of the form is ok):
$dir = './assets/video/';
$config['upload_path'] = $dir;
$config['allowed_types'] = 'avi|flv|wmv|mpeg|mp3|mp4';
$config['max_size'] = '102400';
$this->load->library('upload',$config);
if ( ! $this->upload->do_upload('video')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('data_center/ajout_video', $error);
} else {
redirect('data_center/data_center/','refresh');
}
Concerning mime types, I think it's ok:
'mp4' => 'video/mp4',
'wmv' => array('video/wmv', 'video/x-ms-wmv', 'flv-application/octet-stream', 'application/octet-stream'),
'flv' => array('video/flv', 'video/x-flv', 'flv-application/octet-stream', 'application/octet-stream'),
'avi' => 'video/x-msvideo',
When I try to upload a mp4 file it automatically redirect me (although I'm not sure it's a redirection) to the form, unfilled and no error message, even if I comment the lines in case of upload failure not to load the view again or if the rest of the form is uncorrect (it should not attempt to upload the video and load the form pre-filled).
Whereas for example, if I put the wrong type of file, I can see the error message ('The filetype you are attempting to upload is not allowed' for example), or if the form is wrong I can see the error of the form.
Finally, I must add that I modified php.ini to authorize such big files, and that I did pretty much the same thing with a form for pictures (jpg,jpeg,png...) which works perfectly but also redirect me to an unfilled form if I try to upload a .mp4 file.
Edit: I just downloaded an flv video to try, and it uploaded perfectly fine, but its size was less than 8mb (default config), and a 40mb flv file had the same problem as the mp4 file, although I changed my config in php.ini for 100mb
Efectively you have to change two parameters in PHP ini file
post_max_size = 100M
upload_max_filesize = 100M
But you may also want to change the apache abuse protection parameter (100M)
LimitRequestBody 1073741824
And by another hand, PHP have a time limit too of 30 sec per script, so your script will die at 30 seconds of running.
You may also want to increase the time to be sure your script does not die meanwhile you are uploading, copying etc,
set_time_limit(600); // 10 minutos execution

Zend File upload: File exceeds the defined ini size

Inside my form i define this file upload field:
$this->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
$logo = $this->createElement('file', 'logo');
$logo->setLabel('Group logo')
->setMaxFileSize(5242880) // 5mb
->addValidator('IsImage')
->addValidator('Count', false, 1)
->addValidator('Size', false, 5242880)
->addValidator('Extension', false, array('jpg', 'jpeg', 'png', 'gif'));
However, no matter how small files I upload I get this error: File 'logo' exceeds the defined ini size.
The error message seemed pretty straight forward so I checked the php config (phpinfo() on the same exact page that handles the form)
file_uploads: On
upload_max_filesize: 2000M
memory_limit: 128M
post_max_size: 8M
While those values don't exactly make sense, they absolutely should allow me to upload files up to 8Mb but the upload always failes with the message from above. Even files smaller than 1Kb fail. I also tried removing all setters/validators but it still fails.
While searching for an answer I came across some posts that said that it was ajax' fault but this is a regular form, so now I'm stuck.
Update: I'm terribly sorry to have wasted your time, there was another unclosed form on the page which voided the multipart-declaration. Could have found that out sooner if I had tested with larger files rather than small ones :/
Add enctype="multipart/form-data" in your form. It should solve your problem.
Add
enctype="multipart/form-data"
to your <form> element. Solved my problem.
if you are using script file to render your file , you need to retrieve the enctype info that you specified in form class from your script file. <form enctype="<?php echo $this->element->getAttrib("enctype"); ?>">
Chances are that the php extension fileinfo is not activated.
Please check your php.ini file and increase upload_max_filesize. By default, it is 2M (2 MegaBytes). Also in order to be able to post file with size more than 2M you need to update value of post_max_size
It looks like you're missing the destination:
$logo->setLabel('Group logo')
->setDestination('/var/www/upload')
...
You might want to make sure that the folder is writeable by your web server.
When I commented out the following I got the same error:
->setDestination($this->_config->folder->ugc);
->addValidator(Kvadrat_Form_Element_File::VALIDATE_COUNT, true, 1);
->addValidator(Kvadrat_Form_Element_File::VALIDATE_SIZE, true, 5 * 102400);
(I commented it out as was doing the file uploads separately with FormData)
So I uncommented it and it all worked again.
Your size validator is incorrect. You should use this format:
->addValidator('Size', false, array('max' => '5242880'))
Your validator checks file's size == 5242880, NOT <= 5242880.

Categories