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,075 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 110

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 110

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 110

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 110

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 110

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

738 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