wizo's avatar
Level 4

Change php ini settings for a certain route

I have installed laravel, but I want to change the php.ini settings for a certain route, because I need this route to be able to handle large file uploads. For example, I need to adjust the post_max_size to something like 2G. How can I do this in Laravel or nginx for one laravel install. Is this even possible?

So for example, the route /upload must have a large post_max_size etc but the other routes should just have the normal post_max_size.

0 likes
10 replies
nickywest's avatar

You should be able to use .user.ini files to do per directory changes to php.ini

upload_max_filesize and post_max_size are both PHP_INI_PERDIR, so it should work. I think you'd need a real directory path in public/ though, which is kind of gross.

ABasso's avatar

check that out "upload_max_filesize"

ctroms's avatar

@nickwest. I think you are are correct about needing a real directory

@rsdrsd

If you have one form that required a 2G upload, you can go a head and update your post_max_size to 2G in your php.ini. All that directive does is set a max size so any form can upload up to 2G. It won't effect your applications requests unless a POST request is more than 2G.

If you don't want 2G uploads to be possible on other forms you can be more restrictive on other forms by using Laravel's form validation definitions to set a maximum size like:

'uploaded_file' => 'max:250'

https://laravel.com/docs/5.2/validation#rule-max

wizo's avatar
Level 4

Yeah, I need a real directory, but then I can't use Laravel for handling uploads unless I install a new app. But I don't want that.

I think I have to go with plain php and not use laravel for handling large uploads. Or does some one have other suggestions?

nickywest's avatar
Level 3

You wouldn't need a whole new app, just make a new directory public/big_uploads, copy public/index.php into it and adjust paths as necessary (which would just be adding another ../ to the two required). There could be a little more to it than that, maybe something to do with routes and the base path.

There's no law against having multiple loaders in public paths, it's just kind of weird and normally unnecessary. It's also something to remember when a new dot version of Laravel comes around (you'll have two index.php files that might need updating).

Personally, I'd probably do what @ctroms suggested and just up the file upload size site wide, unless you would have reason to believe you'd get huge file uploads when you don't want to allow them.

Edit: public/.htaccess will definitely need to be edited

Another edit: I just tested this and it's totally viable. Copying both .htaccess and index.php to a subdirectory within public works just fine. No base path issues either (surprised by this a little). You would need to adjust links to prepend the big upload path, and unless you want that path accessible to the entire laravel app, you'll need to account for that somewhere too (the entire app works from the second index.php).

1 like
wizo's avatar
Level 4

Well there are a lot of other forma which don't need the big sizes. I'll give the sub dir a try when the time is there. Thnx!

wizo's avatar
Level 4

The best way for changing ini settings dynamically is not by creating a sub directory, because you'll get all sorts of problems with your routes. For my php fpm setting I created a new block for php handling and added the following fastcgi_param:

fastcgi_param PHP_VALUE "upload_max_filesize=400M \n post_max_size=408M \n max_input_time=10800 \n max_execution_time=10800 \n session.gc_maxlifetime=10800 ";

to your location

vrushabh's avatar

there is php function for changing ini variable put this in your controller logic or make some middle ware to route

// set ini variable for this request

  ini_set('post_max_size','2024M');
    ini_set('upload_max_filesize','2024M');
    ini_set('max_input_time', 36000); // 10 houres
    set_time_limit(36000); // 10 houres

// set ini variable for this request

Please or to participate in this conversation.