Skip to content

Commit

Permalink
Merge pull request #190 from hedgy/FixUploadNoMemoryLimit
Browse files Browse the repository at this point in the history
Fix file upload when memory_limit in php configured to have no limits.
  • Loading branch information
partikule committed Nov 29, 2014
2 parents be09753 + 90a51f1 commit 42b8ed3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions application/libraries/Filemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,10 @@ public function blob_mode_upload()
$max_upload = self::convert_size(ini_get('upload_max_filesize'));
$max_post = self::convert_size(ini_get('post_max_size'));
$memory_limit = self::convert_size(ini_get('memory_limit'));
if( $memory_limit < 0 )
{
$memory_limit = max($max_upload, $max_post);
}
$limit = min($max_upload, $max_post, $memory_limit);

$headers = $this->getHttpHeaders();
Expand Down
4 changes: 2 additions & 2 deletions application/libraries/Filemanager/Image.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct($file)
$this->meta = self::checkFileForProcessing($file);

// only set the new memory limit of IMAGE_PROCESSING_MEMORY_MAX_USAGE MB when the configured one is smaller:
if ($this->meta['fileinfo']['memory_limit'] < IMAGE_PROCESSING_MEMORY_MAX_USAGE * 1024 * 1024)
if ($this->meta['fileinfo']['memory_limit'] > 0 && $this->meta['fileinfo']['memory_limit'] < IMAGE_PROCESSING_MEMORY_MAX_USAGE * 1024 * 1024)
{
ini_set('memory_limit', IMAGE_PROCESSING_MEMORY_MAX_USAGE . 'M'); // handle large images
}
Expand Down Expand Up @@ -243,7 +243,7 @@ public static function checkFileForProcessing($file)
throw new Exception('no_imageinfo');

// only set the new memory limit of IMAGE_PROCESSING_MEMORY_MAX_USAGE MB when the configured one is smaller:
if ($finfo['memory_limit'] < IMAGE_PROCESSING_MEMORY_MAX_USAGE * 1024 * 1024)
if ($finfo['memory_limit'] > 0 && $finfo['memory_limit'] < IMAGE_PROCESSING_MEMORY_MAX_USAGE * 1024 * 1024)
{
// recalc the 'will_fit' indicator now:
$finfo['will_fit'] = ($finfo['usage_min_advised'] < IMAGE_PROCESSING_MEMORY_MAX_USAGE * 1024 * 1024);
Expand Down
3 changes: 3 additions & 0 deletions application/libraries/MY_Image_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ function _checkMemoryBeforeImageCreate($source_path, $dest_width, $dest_height)
// Memory limit from php.ini config file in bytes
$ini_memory_limit = intval(substr(ini_get('memory_limit'), 0, -1)) * 1024 * 1024;

if ( $ini_memory_limit < 0 )
return true;

// First, check the source
$size = getimagesize($this->full_src_path);
$picture_need = $size[0]*$size[1]*(2.2+($truecolor*3)) + memory_get_usage();
Expand Down
4 changes: 4 additions & 0 deletions application/libraries/MY_Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,10 @@ public function get_max_upload_size()
$max_upload = $this->convert_size(ini_get('upload_max_filesize'));
$max_post = $this->convert_size(ini_get('post_max_size'));
$memory_limit = $this->convert_size(ini_get('memory_limit'));
if($memory_limit<0)
{
$memory_limit = max($max_upload, $max_post);
}
$limit = min($max_upload, $max_post, $memory_limit);

return $limit;
Expand Down

0 comments on commit 42b8ed3

Please sign in to comment.