8

When we use ini_set like below in code, will memory limit be the same for all other codes? or only valid for in which code we added ?

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

Thanks

user6618536
  • 207
  • 2
  • 3
  • 11

2 Answers2

5

It is only working for the file where it is written. Best way to change settings for multiple phps in the same folder are:

1. creating a file named ".htaccess" with the line

php_value memory_limit '512M'

Keep in mind, this only works, if you server configuration allowes "AllowOverride" directive for this directory

2. adding a own php with your config options as auto-prepend-file

You can do this in php.ini.

3. changing value of "memory_limit" directly in you php.ini

but this will take affect to ALL php-files you are running

Radon8472
  • 4,285
  • 1
  • 33
  • 41
  • i dont want to change the default memory_limit ,just want to incerease it for specific php file , so when i add ini_set('memory_limit', '512M'); , then only for this file memory will be used to 512. other php file will use default limit . this is what i undertand. – user6618536 Mar 03 '17 at 12:00
  • Yes thats correct, ini_set only affects THIS php and all files wich are included from this file. – Radon8472 Mar 03 '17 at 12:05
  • 1. This solution is tied exclusively to Apache httpd. 2. This solution is slow. 3. This is The Right Way™ to do it (defined as: most portable, with best performance). – Ryan Parman Jun 08 '18 at 21:43
  • 1
    you can change the link from `php.net/manual/de/` to `php.net/manual/en/` –  Nov 12 '19 at 13:30
1

It depends where you are adding.

If in a specific script then till that script is running. If setting at application level then it will be set for that specific application.

Naincy
  • 2,953
  • 1
  • 12
  • 21
  • if i add it in test.php then when we execute it , memory will be setted to 512M then after exiting from script , default memory limit(not 512M) will be used for other php file, right? – user6618536 Mar 03 '17 at 11:50