简体   繁体   English

在 Symfony 中清空上传的文件

[英]Empty uploaded file in Symfony

I want to upload a csv file with a form but if the file is too big (> 200 rows) it doesn't work.我想上传一个带有表单的 csv 文件,但如果文件太大(> 200 行),它就不起作用。

For instance, when I upload a big file and I do:例如,当我上传一个大文件时,我会这样做:

 die(var_dump($request->files->get('file')));

Here is the result:结果如下:

object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(12) "big_file.csv" 
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(24) "application/octet-stream" 
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) 
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(1) ["pathName":"SplFileInfo":private]=> string(0) "" 
["fileName":"SplFileInfo":private]=> string(0) "" }

As you can see, the size is 0, the fileName and the pathName are empty and the mime type is wrong (it should be text/csv).可以看到,大小为0,fileName和pathName为空,mime类型错误(应该是text/csv)。 And when I upload a small file (around 100 rows) it works, there is the good size, the pathName/fileName etc.当我上传一个小文件(大约 100 行)时,它可以工作,有合适的大小、路径名/文件名等。

Do you know if it's a problem with PHP?不知道是不是PHP的问题?

FYI, I have these settings in the PHP code:仅供参考,我在 PHP 代码中有这些设置:

ini_set('upload_max_filesize', '40M');
ini_set('post_max_size', '40M');
ini_set('max_input_time', 600);
ini_set('max_execution_time', 600);
ini_set('memory_limit', '256M');
set_time_limit(0);

Regards问候

The file fails to upload because its size is bigger than permitted in PHP configuration files.该文件无法上传,因为其大小大于 PHP 配置文件中允许的大小。

You can see that by looking at the error that is generated from this method:您可以通过查看此方法生成的错误来了解这一点:

$request->files->get('file')->getErrorMessage()

You can achieve this with a few easy steps.您可以通过几个简单的步骤来实现这一点。

Override php.ini directly:直接覆盖php.ini:

Edit your /etc/php/7.4/apache2/php.ini (instead of 7.4 use your version - how to find your version is explained when you read further, in short write in terminal php -v and use 2 digits eg 7.4)编辑您的/etc/php/7.4/apache2/php.ini (而不是 7.4 使用您的版本 - 当您进一步阅读时会解释如何找到您的版本,简而言之,在终端php -v写入并使用 2 位数字,例如 7.4)

Find there "upload_max_filesize" and "post_max_size" then change the values to (use what ever size you need, I used 20 Megabytes eg 20M):在那里找到“upload_max_filesize”和“post_max_size”,然后将值更改为(使用您需要的任何大小,我使用了 20 兆字节,例如 20M):

upload_max_filesize = 20M 
post_max_size = 20M

Then Save the file and restart Apache in my case the command is:然后保存文件并在我的情况下重新启动 Apache 命令是:

/etc/init.d/apache2 restart

Or you might need to use sudo before:或者您可能需要在之前使用 sudo:

sudo /etc/init.d/apache2 restart

Or in other cases:或者在其他情况下:

sudo service apache2 restart

Should work now.现在应该工作。

Another option is to override php.ini without changing the file directly:另一种选择是覆盖 php.ini 而不直接更改文件:

Echo or Print or Return the following function phpinfo() from your code from the response you will find a column with the name "Scan this dir for additional .ini files" I got there the following path: /etc/php/7.4/apache2/conf.d从响应中的代码中回显或打印或返回以下函数phpinfo()您将找到一个名为“扫描此目录以获取其他 .ini 文件”的列,我到达了以下路径: /etc/php/7.4/apache2/conf.d

If you have trouble to get response from phpinfo() , just write in terminal php -v this will give you the version of your PHP so if you will see there:如果您无法从phpinfo()获得响应,只需在终端php -v写入,这将为您提供 PHP 的版本,因此如果您会在那里看到:

PHP 7.4.10 (cli) ( NTS )
Copyright (c) The PHP Group

Use only the first 2 digits of the version and go to this folder I mentioned /etc/php/7.4/apache2/conf.d (Instead of 7.4 type your 2 digit version)仅使用版本的前 2 位数字并转到我提到的此文件夹/etc/php/7.4/apache2/conf.d (而不是 7.4 键入您的 2 位数字版本)

In your conf.d directory create a file named: file-upload.ini (you can call the file as you like as long as it finishes with .ini).在您的 conf.d 目录中创建一个名为: file-upload.ini的文件(您可以随意调用该文件,只要它以 .ini file-upload.ini )。 This directory files overrides your php.ini settings此目录文件覆盖您的 php.ini 设置

So create the file and write inside (use inside what ever file size you need, I used 20M):因此,创建文件并写入其中(在您需要的任何文件大小中使用,我使用了 20M):

upload_max_filesize = 20M 
post_max_size = 20M

Then Save the file then restart Apache with the commands I mentioned above.然后保存文件,然后使用我上面提到的命令重新启动 Apache。

Now the file should be uploaded successfully.现在文件应该成功上传了。

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

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