Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"Fatal error: Allowed memory size of xxx bytes exhausted" when bytes being allocated is less than allowed bytes?

Posted on 2011-03-02
11
Medium Priority
?
1,087 Views
Last Modified: 2012-05-11
I'm getting the following error on a script that has previously worked:

Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 6144 bytes) in path/user/htdocs/admin/news_post.php on line 81

What I don't understand is that the bytes being allocated is WAY smaller than the limit?!

The script is an image upload script, and this is happening when images above 1MB are being uploaded.

Attached is the relevant portion of the code
$target = "../images/"; 
$target = $target . $randomise . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
$filesize = $_FILES['uploaded']['size'];

if ($filesize > 0) {

//This is our size condition 
if ($filesize > 3500000)  { 
	echo "Your file is too large.<br>"; 
	$ok=0; 
} 

//This is our limit file type condition 

$info = getimagesize($_FILES['uploaded']['tmp_name']);


if(($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) {
	die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG images allowed.");
}


//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
	if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { 
 

		$source_pic = $target;
		$destination_pic = '../images/'. $randomise . urlencode(basename( $_FILES['uploaded']['name']));
		$destination_thumb = '../images/thumbs/'. $randomise . urlencode(basename( $_FILES['uploaded']['name']));
		$max_width = 270;
		$max_height = 2000;

		$src = imagecreatefromjpeg($source_pic);
		list($width,$height)=getimagesize($source_pic);

		$x_ratio = $max_width / $width;
		$y_ratio = $max_height / $height;

		$tn_width = $width;
		$tn_height = $height;

		$tmp=imagecreatetruecolor($tn_width,$tn_height);
		imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

		if(imagejpeg($tmp,$destination_pic,100)){

		$imageuploaded=1;

		}

		if($tn_width<70 && $tn_height<50) {
			$tmp=imagecreatetruecolor($tn_width,$tn_height);
			imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
		}
		else {
			$tmp=imagecreatetruecolor($tn_width,$tn_height);
			imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
		}
		if(imagejpeg($tmp,$destination_thumb,100)){

		$thumbuploaded=1;

		}


	imagedestroy($src);
	imagedestroy($tmp);

	if ($imageuploaded==1 && $thumbuploaded==1) { .. etc. etc.

Open in new window

0
Comment
Question by:freestate
  • 5
  • 4
  • 2
11 Comments
 
LVL 15

Expert Comment

by:ncoo
ID: 35020306
Use ini_set to increase the limit.

It means the allowed limit of 20971520 bytes have all been used up, PHP then tried to use an additional 6144 bytes which it could not do. So your script was trying to use 20971520 + 6144 bytes.
ini_set("memory_limit","2M");

Open in new window

0
 
LVL 15

Assisted Solution

by:ncoo
ncoo earned 600 total points
ID: 35020336
You probably want to set the limit to more then 20M.


20 971 520 bytes = 20 megabytes
ini_set("memory_limit","40M");

Open in new window

0
 

Author Comment

by:freestate
ID: 35020375
Hi,

Am I right in thinking my host (this is a shared server) must have recently dropped the limit available to my account if this has just started happening 'out of the blue'?

Is there anything that can be done to make the image upload script more efficient?

Rob
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:freestate
ID: 35020413
Tried upping to 40MB, but still get an out of memory error.

The file I'm uploading is less than 1.5MB, and so I don't understand how this can be using more than 40MB to be processed?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35022683
The error message said it happened on line 81, but there are only 78 lines of code in your post.  Are you sure you are running the right script?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35022686
Run this script to see the limits:  Look through the part called "Core"

<?php phpinfo();
0
 

Author Comment

by:freestate
ID: 35024834
Sorry - that's not very useful of me....

I cut out some code from the top that wasn't relevant.

This is the line that the error is referring to:

                  $tmp=imagecreatetruecolor($tn_width,$tn_height);
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35032736
OK, maybe if you can show us the complete script we can help.  Is that line by any chance inside a loop?
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1400 total points
ID: 35032740
Also, have you printed out the values of $tn_width and $tn_height to see if they are in the "sane" range?
0
 

Author Comment

by:freestate
ID: 35055230
Ray - you're right with the last point - $tn_width used to be set t othe maximum value, but somehow this code had been stripped out - which meant it was using the original values provided, which were very large.

Thanks!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35057357
Thanks for the points - glad it's looking up! ~Ray
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question