Jump to content

PHP Upload memory_limit


barkster

Recommended Posts

I'm writing a simple upload script and  when I upload a 300k file I get "Allowed memory size of 8388608 bytes exhausted (tried to allocate 2048 bytes) in..." when the file is totally under 8MB?  I've used this before and never had to change any setting in ini file or anything.  Seems to fail on "imagecreatefromjpeg" line.

 

function createthumb($filename,$thumb_path_name,$thumbsize) {
list($width, $height) = getimagesize($filename);			
$imgratio=$width/$height;
if ($imgratio>1){
	 $newwidth = $thumbsize;
	 $newheight = $thumbsize/$imgratio;
}else{
	 $newheight = $thumbsize;
	 $newwidth = $thumbsize*$imgratio;
}				
$thumb = imagecreatetruecolor($newwidth,$newheight);
$source = imagecreatefromjpeg($filename);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if(!imagejpeg($thumb,$thumb_path_name,80)) {
	error_log("\n".date("h:i:s").":Could not create thumbnail: ".$newname,3,"error.txt");
}
}

Link to comment
https://forums.phpfreaks.com/topic/104143-php-upload-memory_limit/
Share on other sites

It hasnt to do with filesize, but memory (RAM) used. Probably ure running gd functions over very big image files, which drains the allowed memory of 8MB. U can increase the allowed memory in php.ini or with -- ini_set("memory_limit","10M"); -- but that shouldnt be a good practice i guess.

Yes, that seems to be what it was I modified it to this and uploaded some 2MB images no problem

 

function createthumb($filename,$thumb_path_name,$thumbsize) {
$source = imagecreatefromjpeg($filename);
list($width, $height) = getimagesize($filename);			
$imgratio=$width/$height;
if ($imgratio>1){
	 $newwidth = $thumbsize;
	 $newheight = $thumbsize/$imgratio;
}else{
	 $newheight = $thumbsize;
	 $newwidth = $thumbsize*$imgratio;
}				
$thumb = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if(!imagejpeg($thumb,$thumb_path_name,80)) {
	error_log("\n".date("h:i:s").":Could not create thumbnail: ".$newname,3,"error.txt");
}
imagedestroy($source);
imagedestroy($thumb);
}

U cant always edit the php.ini file and it is not good option to do so.This can be better done in the client side scripting using the JAVASCRIPT.I suggest you to try this.

 

U cant alwyas edit the php.ini and thats true on shared hosts. Anyway they provide custom php.ini and also using ini_set() wont change the default php.ini, only change that feature on the specific script. As for the client side using javascript, i really dont get what ure talking.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.