How to the change php settings from php code? - php

I want to change the php setting but from ".php" page not php.ini. The settings I want to change is
upload_max_filesize, post_max_size and memory_limit

You can try it with a .htaccess file, if you have AllowOverride Options:
Place a file named .htaccess to your webroot:
php_value upload_max_filesize 10000
php_value post_max_size 10000
php_value memory_limit 10000

The only one of those that can be changed from within PHP is the last one, which can be changed with ini_set like this:
ini_set('memory_limit', '32M');
PHP always processes the client request before the PHP script is started. This means that uploaded files are already uploaded and posted forms are already fully posted beforeb he script starts. The upload and post settings can therefore not be set in the script, ebcause they are already irrelevant when the PHP script is started.

If your server administrator hasn't prevented it, you can use ini_set() to change the memory limit:
ini_set("memory_limit","16000000"); // abbreviations like "16M" work only
// in php.ini, always use full numbers here
The two other options are needed before the PHP script is loaded, there is no way to change those in php.ini.

Use
ini_set ('key', 'value');
Note that not all the available options can be changed using ini_set(). Here'is a list: ini.list
Read more in ini_set reference;

Related

PHP FileInfo Extension php.ini override

I'm making use of a GD Library on my application which requires me to modify my php.ini to work. I understand if I uncomment to extension=fileinfo.so the application will work as required, but my challenge is that I don't have access to my shared host server configurations.
My question, Is there a way around using ini_set('', '')
Thanks ahead
Shared hosting has disabled ini_set() function for security reason
if the hosting provider turn on ini_set() to you, you can have full access to php variable control, which the hosting provider don't want
for your purpose, you can do it through .htaccess file within your application root folder
two directives are permitted using .htaccess
php_flag <boolean-flag-name> on|off
php_value <flag-name> <flag-value>
php_flag should be used for on/off values
For example, the following .htaccess file will disable globals, set the maximum file upload size to 20MB, and allow PHP scripts to run for 10 minutes (600 seconds):
php_flag register_globals off
php_value upload_max_filesize 20M
php_value max_execution_time 600
You can also use ini_set function. in php scripts which allows you to change a setting within your application at runtime. The function accepts two arguments:
ini_set(flag-name, flag-value),
Example
<?php
ini_set('register_globals', 0);
ini_set('upload_max_filesize', '20M');
ini_set('max_execution_time', 600);
?>
We can query the php interpreter before changing these value to query, we can use ini_get() method
ini_get(flag-name)
Returns the configuration value. I’d recommend checking your configuration change and taking appropriate action. Don’t assume ini_get() will always work.
ini_get_all([extension])
Returns all configuration values as an associative array. The optional extension parameter returns options specific to that extension, e.g. ‘allow_url_fopen’.
get_cfg_var(flag-name)
Returns the original configuration value from php.ini (not any overrides set in .htaccess or by ini_set).
ini_restore(flag-name)
Returns a configuration option to its original value.

ini_set doesn't overwrite php settings

I am trying upload larger files so I set this in php code
ini_set('upload_max_filesize', '1000M');
ini_set('post_max_size', '1100M');
ini_set('memory_limit', '1200M');
but $_POST and $_FILES are empty. But when I change php.ini settings it works. Why it doesn't change setting on the fly?
It's too late to have those settings changed after PHP has started.
upload_max_filesize - uploads happen before the PHP script runs and your ini_set() is executed
post_max_size - likewise
And even if you could change memory_limit (depends on server configuration), it would be too late again if you expect that big of an upload.
See ini_set() and http://www.php.net/manual/en/ini.list.php for more info.
Have a look at these :
http://www.php.net/manual/en/ini.list.php
http://www.php.net/manual/en/configuration.changes.modes.php
After reading, it seems that the only option that you can modify with ini_set is memory_limit.
The others 2 being configurable in the php.ini file, in httpd.conf, .htaccess, or in a per directory .user.ini file.
Meaning, if you want to change these values, you must have access to your server's configuration ! You won't be able to afford that from within the script !
The first two settings are not allowed to be changed per script, but even if they were it still would not be effective since the files/POST data must be handled before the script is even run, which means that the settings wouldn't have a chance to take effect.

PHP Empty $_POST

I am sending data to PHP through HTTP POST. This works fine for data shorter than 8MB (8192KB), however when higher quantities of data are sent, PHP shows the $_POST variable to be empty. I emphasize that the $_POST variable does not even contain the names of the post fields, it exists as an empty array. The critical point seems to be between 8.0MB and 9.0MB, and continues higher of course.
I have attempted the following with no success:
ini_set('memory_limit', '500M');
ini_set('post_max_size', '220M');
ini_set('upload_max_filesize', '220M');
I require the data to pass through HTTP POST. The data cannot be uploaded as a file.
Also could Apache be responsible for this?
Any help would be appreciated.
This will help
What are the caveats with increasing max_post_size and upload_max_filesize?
And maybe
http://blurringexistence.net/archives/11-PHPs-max_post_size.html
take a look at the documentation comments. when the script is executed, itäs too late to change sopme setting, wich includes post_max_size, for example. to change these values, try to use a .htaccess-file like this:
php_value upload_max_filesize 200M
php_value post_max_size 200M
or change these settings directly in your php.ini.
post_max_size is, according to the documentation, defined as a PHP_INI_PERDIR setting. It can be set in your php.ini or .htaccess file. A definition for PHP_INI_PERDIR is given here: http://www.php.net/manual/en/configuration.changes.modes.php
Settings that are defined as PHP_INI_ALL can be set with ini_set().

php file size limit

I trying to use PHP's file upload abilities, but when I try to upload certain file sizes (9MB for example) it's not going through. Smaller files goes fine.
Even when I set error reporting to be on "E_ALL" and try and upload a bigger file. I don't see any error message.
I have tried setting these lines at the top of the PHP script that uploads the files but still no go:
ini_set('memory_limit', '96M');
ini_set('post_max_size', '96M');
ini_set('upload_max_filesize', '96M');
Take a look at the list of parameters for PHP and where you can set them. If I read that list correctly, post_max_size and upload_max_filesize can only be set in php.ini, not via ini_set.
Set those values in the appropriate php.ini (there are different ones for CLI and mod_php), or if you don't have access to the main php.ini create a php.ini in your folder.
To check if the settings work, create a phpinfo file and check the values.
try changing the php.ini file which is having certain properties.
you can change all the properties that u have mentioned in your php file into your ini file this worked for me. Try if it works for u as well
Try setting a MAX_FILE_SIZE entry in the form.
<input type="hidden" name="MAX_FILE_SIZE" value="100663296" />
where the number is in bytes. Additionally, try changing the PHP settings in either php.ini or a .htaccess file in the same directory. The syntax for php settings in htaccess files are:
php_value name value
So you would say:
php_value memory_limit 96M
php_value post_max_size 96M
php_value upload_max_filesize 96M
Also, are you using your own server, or some online host. Some online hosts set a maximum file upload limit that you have to change in the Control Panel (depending on what interface the host uses), or they just plain don't let you change the max file size.
I don't see any error message.
that's because file uploads has it's own error messages. check $_FILES['filename']['error']
I have tried setting these lines
but you didn't check if it has any effect! run phpinfo() to see actual values

How to change php setting from php file? [duplicate]

I want to change the php setting but from ".php" page not php.ini. The settings I want to change is
upload_max_filesize, post_max_size and memory_limit
You can try it with a .htaccess file, if you have AllowOverride Options:
Place a file named .htaccess to your webroot:
php_value upload_max_filesize 10000
php_value post_max_size 10000
php_value memory_limit 10000
The only one of those that can be changed from within PHP is the last one, which can be changed with ini_set like this:
ini_set('memory_limit', '32M');
PHP always processes the client request before the PHP script is started. This means that uploaded files are already uploaded and posted forms are already fully posted beforeb he script starts. The upload and post settings can therefore not be set in the script, ebcause they are already irrelevant when the PHP script is started.
If your server administrator hasn't prevented it, you can use ini_set() to change the memory limit:
ini_set("memory_limit","16000000"); // abbreviations like "16M" work only
// in php.ini, always use full numbers here
The two other options are needed before the PHP script is loaded, there is no way to change those in php.ini.
Use
ini_set ('key', 'value');
Note that not all the available options can be changed using ini_set(). Here'is a list: ini.list
Read more in ini_set reference;

Categories