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

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
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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 AnalystAuthor 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 ProblemsCommented:
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.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Julian HansenCommented:
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 AnalystAuthor Commented:
Good...

It's the same for the other directives:  max_execution_time  and post_max_size ?
Dave BaldwinFixer of ProblemsCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.