I am trying to change post_max_size using ini_set(), but it is not working. After some research I concluded that this is due the time post_max_size is applied.
So what to do in order to change the post_max_size without having to manually edit php.ini nor .htaccess
My script is an upload form, and it must handle large files, and I only want these settings to be applied on it, not all other scripts.
Not possible, unfortunately. post_max_size (and upload_max_filesize) are used before your script is started, so the only way to set them is in .htaccess or php.ini.
Then how can I modify .htaccess through the PHP code?, just like we modify php.ini using ini_set()
Unless PHP has write permissions for .htaccess (which it shouldn't), you won't be able to do this with PHP. You'll have to download the .htaccess file, edit it in Notepad or something similar and then reupload it.
Most webhosting providers will allow you to change (some) php settings via .htaccess files. Here is what you need to add:
php_value post_max_size 52428800 # 50MB
php_value upload_max_filesize 52428800 # 50MB
php_value max_execution_time 300 # 5 minutes
After making these changes to .htaccess file, double check using:
<?php phpinfo(); ?>
Look at the local values.
PS: This works if PHP runs as an APache module. If your web hosting setup is different, you must ask them about how to do this. AFAIK, it is possible to put (some of) the settings in a php.ini file and upload to the root directory of your website but depends on how PHP and Apache are setup.