Thursday, August 20, 2009

how to increase upload_max_filesize, post_max_size and and memory_limit without accessing php.ini

When I was making a script to upload files, I encountered problems like I cannot upload files greater than 2MB. When I checked upload_max_filesize, post_max_size, and memory_limit there value are very low. I have this result when I executed this script in php


<?php

function convertBytes( $value ){
if (is_numeric( $value )) {
return $value;
}else{
$value_length = strlen( $value );
$qty = substr( $value, 0, $value_length - 1 );
$unit = strtolower( substr( $value, $value_length - 1 ) );
switch ( $unit ) {
case 'k':
$qty *= 1024;
break;
case 'm':
$qty *= 1048576;
break;
case 'g':
$qty *= 1073741824;
break;
}
return $qty;
}
}
echo 'Maximum file size: '.convertBytes(ini_get( 'upload_max_filesize' ))
/1048576.'MB'.'
';
echo 'post_max_size: '.convertBytes(ini_get('post_max_size'))
/1048576 . 'MB'.'
';
echo 'memory_limit: '.convertBytes(ini_get('memory_limit' ))
/1048576 . 'MB'.'
';
?>

Output:

Maximum file size: 2MB
post_max_size: 8MB
memory_limit: 16MB




To increase your upload_max_filesize, post_max_size, and memory_limit you can manipulate it using an .htaccess file. The first thing you do is to create an .htaccess file or if you already have just include this script in your .htaccess.

php_value upload_max_filesize "32M"
php_value post_max_size "32M"
php_value memory_limit "128M"


Important: PHP modifications only apply to the parent directory and its children. If the .htaccess file were placed in /var/www/html, then the changes would apply to the document root and recursively to all directories within.

You can now again run the php script above in order to see the changes.

No comments: