简体   繁体   中英

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.

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). And when I upload a small file (around 100 rows) it works, there is the good size, the pathName/fileName etc.

Do you know if it's a problem with PHP?

FYI, I have these settings in the PHP code:

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.

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:

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)

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 = 20M 
post_max_size = 20M

Then Save the file and restart Apache in my case the command is:

/etc/init.d/apache2 restart

Or you might need to use sudo before:

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:

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

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:

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)

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). This directory files overrides your php.ini settings

So create the file and write inside (use inside what ever file size you need, I used 20M):

upload_max_filesize = 20M 
post_max_size = 20M

Then Save the file then restart Apache with the commands I mentioned above.

Now the file should be uploaded successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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