简体   繁体   English

Symfony2-maxSize参数不适用于文件上传

[英]Symfony2 - maxSize parameter not working on file upload

I have a file uploading function on my Symfony2 project. 我在Symfony2项目中具有文件上传功能。

I am seting the maxSize parameter like that: 我正在像这样设置maxSize参数:

$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: 问题是,当我尝试上传2M或3M字文件时,我收到了验证消息:

 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文档中获取了示例:

Symfony File - Validation Constraints Reference Symfony文件-验证约束参考

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. 这是Symfony的一个已知错误 ,实际上,当文件大小对于您的PHP配置而言过大时,框架也会显示验证器错误消息,而不是出现经典的PHP错误。

In your current PHP config, you probably limited the max upload size to 2M, so Symfony display the wrong error. 在当前的PHP配置中,您可能将最大上传大小限制为2M,因此Symfony显示错误错误。

So check your php.ini file (/etc/php5/apache2/php.ini on Linux) and increase max_upload_size to fit your field : 因此,请检查您的php.ini文件(在Linux上为/etc/php5/apache2/php.ini),并增加max_upload_size以适合您的领域:

upload_max_filesize = 20M

Don't forget to restart apache : apache2ctl 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) ^^ 请注意,这可能已在最新的Symfony版本上修复,另一种解决方案可能是将项目升级到sf2.3(但我不确定)。

I created a jQuery validation method to prevent sending big files to server because php don't valide it (It is in Spanish): 我创建了一个jQuery验证方法来防止将大文件发送到服务器,因为php不能验证它(它是西班牙语):

$(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');
        }
    });
}

如果您使用POST上传文件,请当心php.ini中的post_max_size限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM