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
1,068 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 150 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 109

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 109

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 109

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 109

Accepted Solution

by:
Ray Paseur earned 350 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 109

Expert Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHPStorm debugging issues 1 44
Wordpress Taxonomy 2 27
UPDATE query not working in mysqli php 8 49
how to use 2 SELECT statements with prepared statements 19 28
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

785 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