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,071 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
PHP strings vs array errors 13 32
Undefined variable problem 5 21
MSSQL - Lock Row from reading by other programs 9 34
Reference key in foreach loop 4 15
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

789 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