Trouble with ini_set("memory_limit") in php 5.3 - php

I am attempting to use ini_set("memory_limit", "256M"); to set the memory limit to 256M for my script, but it doesn't seem to work.
echo ini_get("memory_limit");
gives me 128M which is the default.
Using ini_set to change the max execution time does when tested the same way.
Having already found this question I checked the suhosin.ini and php.ini files. Suhosin caps it at 2048M though php.ini limits that to 512.
I am not running into either of those caps, and there is another script in the same directory running with a memory limit of 512MB, so there isn't anything in the .htaccess. Is there somewhere else I should be checking?

Related

Comment résoudre ce problème lié à l'installation de glpi sur wampserver? [duplicate]

How to increase transaction timeout? I want to upload videos, but large size of videos not uploaded?
It throws error The process *** exceeded the timeout of 60 seconds.
You need to change some settings in your php.ini:
upload_max_filesize = 2M
;or whatever size you want
max_execution_time = 60
; also, higher if you must - sets the maximum time in seconds
Where your PHP.ini is located depends on your system environment. For more information: http://php.net/manual/en/ini.list.php
You should be able to do during runtime too using
set_time_limit(100);
http://php.net/manual/en/function.set-time-limit.php
or in your vhost-config
php_admin_value max_execution_time 10000
Having a global execution time limit that is LOW is mostly a good idea for performance-reasons on not-so-reliable applications. So you might want to only allow those scripts to run longer that absolutely have to.
p.s.: Dont forget about post_max_size and upload_max_filesize (like the first answer told allready)
To complete the answer of Hannes.
You need to change some setting in your php.ini:
upload_max_filesize = 2M
;or whatever size you want
max_execution_time = 60
; also, higher if you must
If someone want put in unlimited (I don't know why but if you want), you can set the time to 0:
You need to change some setting in your php.ini:
upload_max_filesize = 0
max_execution_time = 0
And if you don't know where is your php.ini. You can do a file "name.php" in your server and put:
<?php phpinfo(); ?>
And on your website, you can see the config of your php.ini and it's marked where is it.
Edit on 9 January 2015:
If you can't access your php.ini, you have two more options.
You can set this line directly in your "name.php" file but I don't find for upload_max_filesize for this option:
set_time_limit(0);
Or in ".htaccess"
php_value upload_max_filesize 0
php_value max_execution_time 0
if what you need to do is specific only for 1 or 2 pages i suggest to use set_time_limit so it did not affect the whole application.
set_time_limit(some_values);
but ofcourse these 2 values (post_max_size & upload_max_filesize) are subject to investigate.
you either can set it via ini_set function
ini_set('post_max_size','20M');
ini_set('upload_max_filesize','2M');
or directly in php.ini file like response above by Hannes, or even set it iin .htaccess like below
php_value upload_max_filesize 2M
php_value post_max_size 20M
If you happen to be using Microsoft IIS server, in addition to the php.ini settings mentioned by others, you may need to increase the execution timeout settings for the PHP FastCGI application in the IIS Server Manager:
Step 1) Open the IIS Server Manager (usually under Server Manager in the Start Menu, then Tools / Internet Information Services (IIS) Manager).
Step 2) Click on the main connection (not specific to any particular domain).
Step 3) Under the IIS section, find FastCGI Settings (shown below).
Step 4) Therein, right-click the PHP application and select Edit....
Step 5) Check the timeouts (shown below).
In my case, the default timeouts here were 70 and 90 seconds; the former of which was causing a 500 Internal Server Error on PHP scripts that took longer than 70 seconds.
As an addition to above answers, you may use set_time_limit() function:
http://php.net/manual/en/function.set-time-limit.php
passing 0 as an argument will make your script run with no time limit.
If you cannot edit php.ini (on your server for example) you can attempt to change the php.ini parameters from within your php code. Try:
ini_set('max_execution_time', 'NUMBER OF SECONDS TO ALLOW BEFORE TIMEOUT');
If that doesn't work, try also setting 'set_time_limit' in the same way, beyond that I'd say your only option is to contact your host. These settings cannot be modified while in safe mode.
You had a typo: ini_set('max_input_time','200M') - value set needs to be an int, like ini_set('max_input_time','200')
I know you are specifically asking about the PHP timeout, but what no one else seems to have mentioned is that there can also be a timeout on the webserver and it can look very similar to the PHP timeout.
So if you have tried:
Increasing the timeout in php.ini by adding a line: max_execution_time = {number of seconds i.e. 60 for one minute}
Increasing the timeout in your script itself by adding: ini_set('max_execution_time','{number of seconds i.e. 60 for one minute}');
And you have checked with the phpinfo() function that max_execution_time has indeed be increased, then you might want to try adding this to .htaccess which will make sure Apache itself does not time out:
RewriteRule .* - [E=noabort:1]
RewriteRule .* - [E=noconntimeout:1]
More info here:
https://www.antropy.co.uk/blog/php-script-keeps-timing-out-despite-ini-set/
First check the php.ini file path by phpinfo(); and then changed PHP.INI params:
upload_max_filesize = 1000M
memory_limit = 1500M
post_max_size = 1500M
max_execution_time = 30
restarted Apache
set_time_limit(0); // safe_mode is off
ini_set('max_execution_time', 500); //500 seconds
Note: you can also use command to find php.ini in Linux
locate `php.ini`
Test if you are is safe mode - if not - set the time limit (Local Value) to what you want:
if(!ini_get('safe_mode')){
echo "safe mode off";
set_time_limit(180);// seconds
phpinfo();// see 'max_execution_time'
}
*You cannot set time limit this way if safe mode 'on'.
optional : if you set config about php.ini but still can't upload
-this is php function to check error code
$error_type = [];
$error_type[0] = "There is no error";
$error_type[1] = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
$error_type[2] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
$error_type[3] = "The uploaded file was only partially uploaded.";
$error_type[4] = "No file was uploaded.";
//$error_type["5"] = "";
$error_type[6] = "Missing a temporary folder. Introduced in PHP 5.0.3.";
$error_type[7] = "Failed to write file to disk. Introduced in PHP 5.1.0.";
$error_type[8] = "A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.";
//------------------------------
//--> show msg error.
$status_code = $_FILES["uploadfile"]["error"];
if($status_code != 0){
echo $error_type[$status_code];
exit;
}
You can also set a max execution time in your .htaccess file:
php_value max_execution_time 180
To really increase the time limit i prefer to first get the current value. set_time_limit is not always increasing the time limit. If the current value (e.g. from php.ini or formerly set) is higher than the value used in current call of set_time_limit, it will decrease the time limit!
So what's with a small helper like this?
/**
* #param int $seconds Time in seconds
* #return bool
*/
function increase_time_limit(int $seconds): bool
{
return set_time_limit(max(
ini_get('max_execution_time'), $seconds
));
}
// somewhere else in your code
increase_time_limit(180);
See also: Get max_execution_time in PHP script

PHP - Unable to change memory_limit on php-fpm

It might seem a stupid question, but I'm already trying since one hour to figure it out without success.
As stated in title, I'm actually unable to change memory_limit directive in PHP 7.1.15 but only for php-fpm. In fact if I execute php -i | grep memory_limit in terminal I can see the correct value memory_limit => -1 => -1.
What I tried so far:
Setting memory_limit in php.ini
Using ini_set('memory_limit', -1); directly in PHP script
I also tried to disable all loaded extensions cause I thought maybe one of them is overwriting that setting, but didn't work.
Also consider that edited php.ini file is the correct one since I was able to change max_execution_time without any problem.
So how this code
ini_set('memory_limit', -1);
echo ini_get('memory_limit');
exit;
can return 128M as output?
To change the memory limit for PHP-FPM, add the following line to your php-fpm.conf file:
php_admin_value[memory_limit] = -1
from fpm-config
; php_value/php_flag - you can set classic ini defines which can
; be overwritten from PHP call 'ini_set'.
; php_admin_value/php_admin_flag - these directives won't be overwritten by
; PHP call 'ini_set'
You must chose better solution for you. I'm prefer set lower memory limit in global config, and in places (wo I can't rewrite better) I'm add ini_set('memory_limit', xxx);

PHP- cannot change max_execution_time in xampp

I've tried everything to change the max_execution_time of a php crawler script so that it can run an infinite amount of time.
I have changed the php.ini file setting max_execution_time to 0 or 100000000 but with no change
I've also tried setting it from the php script itself by using ini_set('max_execution_time', 0);
All php scripts throw the same error Fatal error: Maximum execution time of 3000 seconds exceeded, what could I be missing and how can I make sure there is no max execution time limit?
php script
<?php
ini_set('MAX_EXECUTION_TIME', -1);
error_reporting(E_ALL); // turn on all errors, warnings and notices for easier debugging
//ini_set('max_execution_time', 123456);
ini_set('max_input_time', -1);
ini_set('memory_limit', '512M');
set_time_limit(0);
date_default_timezone_set('Europe/London');
/*code which scrapes websites*/
?>
phpinfo()
max_execution_time 0 0
max_input_time -1 -1
Try turning off safe mode in php and then try the below code
if( !ini_get('safe_mode') ){
set_time_limit(0); //this won't work if safe_mode is enabled.
}
This should allow you to run the script for infinite time.
In Apache you can change maximum execution time by .htaccess with this line
php_value max_execution_time 200
set_time_limit() php manual ref.
You shouldn't let your crawler run under apache, it's better to run it stand-alone via cli as part of a Gearman setup.
That way it won't hog your web server and it can run as long as you want. You can find many bindings for Gearman that you can use, including PHP of course.
use
set_time_limit(0);
at the top of the script
In WAMP there is three PHP.ini files so you might find 3 in xampp also, so just search for it with find and replace max_execution_time to what you are setting. But you must keep something small not too large as for speedy the app you running.
You could also try setting ignore_user_abort(TRUE); in your script as it might be the browser timing out rather than the script.
From the php.net manual
<?php
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
set_time_limit(0);
See here for more info
http://www.php.net/manual/en/function.ignore-user-abort.php
If you are on windows, and this is a CLI run script maybe read this.
check phpinfo() from a temp script and search for max_execution_time. make sure that it has same value what you are setting. default should be 30 seconds. try to change it to a couple of different values and restart apache then check the value in phpinfo() to confirm.
if when you change the value it is reflected properly in the phpinfo() it means that there is some code in your script which is changing this value. search for two things in your code:
search your code for ini_set() and check if it is change max_execution_time
search for set_time_limit()
these functions can change maximum time limit of execution from script. otherwise you should check .htaccess from where this value may be set. but this will effect phpinfo() also.
I found the following in the xampp documentation. Maybe you are trying to edit the wrong php.ini file?
Why have changes in my php.ini no effect?
Since XAMPP 1.7.1 the "php.ini" is only in the directory "\xampp\php". Till XAMPP 1.7.0 is was in the directory "\xampp\apache\bin".
If a change in the "php.ini" have no effect, it's possible PHP is still using an other one. You can verify this with phpinfo(). Go to the URI localhost/xampp/phpinfo.php and search for "Loaded Configuration File". This value shows you the "php.ini" PHP is really using.
Info:
After changing the "php.ini" you have to restart Apache, thus Apache/PHP can read the new settings.
what you are doing is just setting the max_execution_time to whatever inside your page.
you can't change this using ini_set. you can change the memory_limit only.
see more details here...
from the php official site.
if you want them to be changed, change in php.ini.
You have to change both of these in you php.ini ( and check if that's the right php.ini by finding the location in phpinfo(); output! )
max_execution_time = 0
max_input_time = 0
And after that check if some php file is not overwriting those variables locally.
open php.ini notepad file and search or find upload_max_filesize = 1000M and you should change on Post_max_filesize = 1000M then restart your xampp and refresh the local phpmyadmin..

Reading a File with PHP gives 500 error

I have a script which reads a mp3 file with readfile() (I have also tried fread() with a loop). And on one particular file I am getting a 500 error. All other files seem to read just fine.
When I review the error log on my server I notice I am getting a PHP memory error when ever this particular file is attempted to be read.
Can anyone give me some pointers as to what might be going on?
You already pointed out the problem - PHP is running out of memory. Maximum memory usage in PHP is limited by an ini setting.
You can set it at runtime at the top of your script with ini_set:
ini_set('memory_limit', '64M');
Or you can set it in your php.ini file so it takes effect for all scripts:
memory_limit = 64M;
Finally, you can see the memory used by the currently executing script with memory_get_usage()
The mp3 file is of a larger filesize than memory_limit. You'll need to increase the amount of memory PHP can access if you want to read from this mp3 file.
If you have access to the php.ini file, you can find memory_limit = 16M ; and replace the value with however much memory you need.
If you don't have php.ini access and you do have .htaccess access, add:
php_value memory_limit 16M
and replace the value.
If you have neither, try compressing the mp3 file or otherwise reducing the amount of memory it will take for you to perform this action. Try clearing variables which are unused and take up large amounts of memory.
Well, php is probably running out of memory before completing the script. You can just increase the memory php is allowed to run by changing the memory_limit option in your php.ini.
Try increasing the execution time:
set_time_limit(300); //300 seconds
ini_set('memory_limit','16M'); //increase to some value like 16 MB
Most likely it exceeds maximum memory. This can be adjusted in the php.ini, check here: http://www.php.net/manual/en/ini.core.php#ini.memory-limit

Upper memory limit for PHP/Apache

I'm getting the error when I run my PHP script....
Fatal error: Out of memory (allocated 1827405824) (tried to allocate 88800 bytes)
I've added this line to my PHP script..
ini_set("memory_limit","3000M");
This statement does seem to correctly control the memory usage, but I dont seem to be able to get it above about 1.8GB.
Its as if the upper memory limit is being restricted somewhere else.
I've also added to the php.ini...
memory_limit = 3000M
Does anyone know if the memory is restricted elsewhere?
I'm running a local server with Xampp.
I have Windows 7, 64-bit with 4GB RAM.
My script uses PHP's GD image library and I get the error when trying to allocate an image reference with ImageCreateTrueColor().
(I know this is a huge amount of memory - but this is just a one-of script, and its just a lot easier to do it this way.)
Thanks.
Update....
#elusive #Orbling
I expect everybody's bored whith this question, but here is the simplified code which illustrates the problem.
<?php
ini_set("memory_limit","4000000000");
echo "ini_get = " . ini_get('memory_limit') . "<br>\n";
echo "memory_get_usage = " . memory_get_usage(true) . "<br>\n";
$bigImageHandle = imagecreatetruecolor(22200, 24800); //this is line 5
?>
Browser output...
ini_get = 4000000000
memory_get_usage = 524288
Fatal error: Out of memory (allocated 1843396608) (tried to allocate 88800 bytes) in
E:\User\My_Webs\experiments\houseshunting\temp\osMaps\t1.php on line 5
I tested this out with a smaller set of tiles and the memory used by imagecreatetruecolor() and I estimate I need 2.7GB
Using Acquia Dev Desktop, I had many memory limit crashes.
After having increased the memory limit into PHP.ini.
php_value memory_limit 1024M
php_value max_execution_time 3000
This issue was less frequent but still occuring ( Especially with Feature Recreate )
Into my httpd.conf I increased the StackThread to 16M
ThreadStackSize 16*1024*1024
And it solved the memory crash issue.
Hope it can help
You're running on a 64-bit operating system, but Apache and PHP are likely still 32-bit. If you're using mod_php, apache would be the limiting factor here.
32-bit processes are limited about 2GiB of RAM unless you used the /3GB switch and the software is aware of 3GB support.
That still leaves up about 200 MiB that seems unused, but its small enough that it can be used by various libraries that all have to be loaded in memory
As far as I know, the library usage won't show up in the committed memory, but still counts towards the 2GiB limit (much like device memory counts towards the 4GiB limit on 32-bit windows. Where installing 2 GiB graphics card brings you down to under 2GiB of usable RAM).
Most likely solution? Install a 64-bit PHP, and then dispatch it to that (using a system() call, perhaps)
In Ubuntu 18.04
Check version PHP: php -v
In my case I have PHP 7.4
Edit file: nano /etc/php/7.4/apache2/php.ini
Search and change memory_limit = 2048M
Edit file: nano /etc/php/7.4/cli/php.ini
Search and change memory_limit = 2048M
Finally: systemctl restart apache2
Which PHP version are you using?
The memory_limit variable is, or was, contained in a 32-bit integer, so can not go above 2GB.
See: http://bugs.php.net/bug.php?id=39132&edit=1
From the bottom comment on that bug report, it might be the routine that translates the human readable form to a number, try putting it in digits.
Check your Apache config (e.g., httpd.conf). There's likely an RLimitMEM directive limiting the memory allow for children processes handling requests.
So, you can set your PHP limit all you want, if Apache spawns the process with a memory limit, you can't exceed that.
If you're on a hosted service and have a shared server, likely you don't have access to this config and need to work with your provider. As you can see, it's configuration that applies server-wide... you're not likely going to get them to change this. Then again, if you're looking to spawn bigger than 1.5Gig processes, you prolly should be either solving the problem a different way (others have suggested this) or getting a dedicated server of some kind (e.g. EC2).
For example:
/usr/local/apache/conf
#RLimitMEM 85643200 104857600 # Limit to: 80Mb / process, 100Mb total
RLimitMEM 134217728 537395200 # Limit to: 128Mb / Process, 512Mb total
The issue is likely to be caused by running 32-bit apache and php. Try upgrading these to 64-bit binaries and see if that fixes the issue.
I've had exactly the same problem and after searching a lot I discovered it had nothing to do wih memory limit but with a bug in my code: I had a function using an array, located in external file. In this function I had set the array as "global" but missed to include the file with tis array....
Like the spiderplant0's error, this was giving me an error with a very very huge memory allocation.
So check your code and try to see which part create this error.
Try this
set_time_limit(300);
ini_set('memory_limit', '20000M');
Try this:
#php_value memory_limit 300M
#php_value upload_max_filesize 200M
#php_value post_max_size 200M
#php_value max_execution_time 80000
#php_value max_input_time 80000
try ini_set('memory_limit', '-1');

Categories