Skip to content

Instantly share code, notes, and snippets.

@daftspunk
Created April 1, 2014 03:05
Show Gist options
  • Select an option

  • Save daftspunk/9906962 to your computer and use it in GitHub Desktop.

Select an option

Save daftspunk/9906962 to your computer and use it in GitHub Desktop.
PHP Increase memory limit (set_memory_limit)
<?php
/**
* Increase memory limit if not already
* @param $limit Amount in megabytes
*/
private function increaseMemoryLimit($limit = 512)
{
$limitBytes = $limit * 1048576;
$currentLimit = trim(ini_get('memory_limit'));
$lastChar = strtolower($currentLimit[strlen($currentLimit)-1]);
switch($lastChar) {
case 'g':
$currentLimit *= 1024;
case 'm':
$currentLimit *= 1024;
case 'k':
$currentLimit *= 1024;
}
if ($currentLimit < $limitBytes)
ini_set('memory_limit', $limit . 'M');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment