What is the better way of increase memory limit within PHP code?

Eduardo Fuerte
Eduardo Fuerte used Ask the Experts™
on
Hi Experts!

I recently increased the directive  memory_limit in PHP.INI to permit a 267M file download, in test server.

When asking to the infrastructure support people to make the same in production server the suggestion was to increase memory limit just when it's necessary, in PHP code, and then restore to the anterior limit.

What is the better way in doing that ? Better, by using Codeigniter?

And then how to obtain by code the current memory_limit before to increase it and to restore it after?

Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You can use 'ini_set' with 'memory_limit' at the top of your script.  
http://php.net/manual/en/function.ini-set.php
http://php.net/manual/en/ini.core.php#ini.memory-limit
Eduardo FuerteDeveloper and Analyst

Author

Commented:
I guess that way

ini_set( 'memory_limit', '1024M' );

And how to obtain the memory_limit directive before the operation, to restore it then?
Is it possible programatcically?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You don't have to restore it.  When you use 'ini_set', it is only valid for that page.  Just put the 'ini_set' command at the top of the page.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
ini_set's return value is the old value of the setting before the change. So

$oldvalue = ini_set('memory_limit','1024M');

//Restore

ini_set('memory_limit', $oldvalue);

Open in new window

Eduardo FuerteDeveloper and Analyst

Author

Commented:
Good...

It's the same for the other directives:  max_execution_time  and post_max_size ?
Fixer of Problems
Most Valuable Expert 2014
Commented:
Anything on this page http://php.net/manual/en/ini.list.php that has PHP_INI_ALL can be put in an 'ini_set' command.  'post_max_size' is not one of them.  Since that has to be established Before the script is run, that makes sense.

Note also that it may not do what you want in an 'ini_set' command.  You need to see what it does before you use it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial