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,061 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
 

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 108

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 108

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 108

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 108

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 108

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now