Jump to content

Fatal error: Allowed memory size... BUT, not the typical problem


jonw118

Recommended Posts

So I have a script that uploads 1 image. I changed the script to upload 2 images.

 

When I try to upload 2 of them, I get: Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 525 bytes)

 

SO... I did the typical solution and contacted my host and they changed the memory limit to 16M and issued this comman /etc/init.d/httpd restart.

 

Now, I can upload two images that are very small. But anything of a little size (ex, I tried to upload two 1mb images) and it erred.

 

Is there anything crazy in my code that could be doing this.

 

I'd appreciate any help:

 

The input page:

<?
include ("define.php");
include ("func.php");
$subject=addslashes($subject);

$date=date("Y-m-d H:i:s");


        $ph=date("his").$_FILES['photo1']['name'];
        $fileName=$_FILES['photo1']['name'];
        $ext = substr($fileName, strrpos($fileName, '.') + 1);
        if(($ext!="php") and  ($ext!="html") and ($ext!="htm"))
        
        @move_uploaded_file($_FILES['photo1']['tmp_name'], "./photos/".$ph);
        if(!empty($_FILES['photo1']['name']))
{       $image="./photos/".$ph ;

$newfilename="./photos/".$ph;
$thumbname="./photos/thumb_".$ph;

            switch($_FILES["photo1"]['type'])
             {
                 case 'image/jpeg':
                 case 'image/pjpeg':
                    createthumbnailjpg($newfilename,$thumbname,175,0);
                 break;
                 case 'image/png':
                 case 'image/x-png':
                     createthumbnailpng($newfilename,$thumbname, 175,0);
                     
                 break;
                 case 'image/gif':
                     createthumbnailgif($newfilename,$thumbname, 175,0);
                     
                 break;
}
}


$ph2=date("his").$_FILES['photo2']['name'];
        $fileName2=$_FILES['photo2']['name'];
        $ext = substr($fileName, strrpos($fileName2, '.') + 1);
        if(($ext!="php") and  ($ext!="html") and ($ext!="htm"))
        
        @move_uploaded_file($_FILES['photo2']['tmp_name'], "./photos/".$ph2);
        if(!empty($_FILES['photo2']['name']))
{       $image2="./photos/".$ph2 ;

$newfilename2="./photos/".$ph2;
$thumbname2="./photos/thumb_".$ph2;

            switch($_FILES["photo2"]['type'])
             {
                 case 'image/jpeg':
                 case 'image/pjpeg':
                    createthumbnailjpg($newfilename2,$thumbname2,175,0);
                 break;
                 case 'image/png':
                 case 'image/x-png':
                     createthumbnailpng($newfilename2,$thumbname2, 175,0);
                     
                 break;
                 case 'image/gif':
                     createthumbnailgif($newfilename2,$thumbname2, 175,0);
                     
                 break;
}
}


        $ph=date("his").$_FILES['doc1']['name'];
        $fileName=$_FILES['doc1']['name'];
        $ext = substr($fileName, strrpos($fileName, '.') + 1);
        if(($ext!="php") )
        
        @move_uploaded_file($_FILES['doc1']['tmp_name'], "./docs/".$ph);
        if(!empty($_FILES['doc1']['name']))
        $doc="./docs/".$ph ;

$sql="select id from inputinfo";
$rez=mysql_query($sql,$dblnk);
$nr=mysql_num_rows($rez);
$nr++;

$sql="insert into inputinfo(subject,`description`,document,image,image2,date_submitted,thumb,thumb2,category,rank) values('$subject','$area2','$doc','$image','$image','$date','$thumbname','$thumbname2','$category','$nr')";
$rez=mysql_query($sql,$dblnk);

header('location: output.php');
?>

 

The function code:

<?

function resizeCreatedImage(&$src_img,$outWidth,$outHeight)
{
   $srcWidth=imagesx($src_img); 
   $srcHeight=imagesy($src_img);
   //   if(!$outHeight || $outHeight=='') $outHeight=$outWidth;
$outHeight=($outWidth*$srcHeight)/$srcWidth;
   $imgOut=imagecreatetruecolor($outWidth,$outHeight);

      $xoffset = 0;
   $yoffset = 0;
   if ($srcWidth*$outHeight > $srcHeight*$outWidth)
   {
       $xtmp = $srcWidth;
       $xratio = 1-((($srcWidth/$srcHeight)-($outWidth/$outHeight))/2);
       $srcWidth = $srcWidth * $xratio;
       $xoffset = ($xtmp - $srcWidth)/2;
   }
   elseif ($srcHeight/ $outHeight > $srcWidth / $outWidth)
   {
       $ytmp = $srcHeight;
       $yratio = 1-((($outWidth/$outHeight)-($srcWidth/$srcHeight))/2);
       $srcHeight = $srcHeight * $yratio;
       $yoffset = ($ytmp - $srcHeight)/2;
   }

   imagecopyresampled($imgOut, $src_img, 0, 0, $xoffset, $yoffset, $outWidth, $outHeight, $srcWidth, $srcHeight);
         return $imgOut;
}

function createthumbnailjpg($image_path,$thumb_path,$thumb_width,$thumb_height=0)
{
   $src_img = imagecreatefromjpeg($image_path);
    $dst_img = resizeCreatedImage($src_img,$thumb_width,$thumb_height);
   imagejpeg($dst_img, $thumb_path);
   return true;
}
function createthumbnailpng($image_path,$thumb_path,$thumb_width,$thumb_height=0)
{
   $src_img = imagecreatefrompng($image_path);
    $dst_img = resizeCreatedImage($src_img,$thumb_width,$thumb_height);
   imagepng($dst_img, $thumb_path);
   return true;
}
function createthumbnailgif($image_path,$thumb_path,$thumb_width,$thumb_height=0)
{
   $src_img = imagecreatefromgif($image_path);
    $dst_img = resizeCreatedImage($src_img,$thumb_width,$thumb_height);
   imagepng($dst_img, $thumb_path);
   return true;
}

?>

 

 

Most likely the browser timed out. Anything over 2MB php has a hard time handling due to the browser timing out and the fact that the default upload_max_filesize is 2MB.

 

<?php ini_set("upload_max_filesize", "16M"); ?>

 

Try adding that somewhere and see if it allows your upload. Also remember slow connections can make the browser timeout and if the uploads take longer than 30 seconds, php will time out unless you set the set_time_limit

 

I would read up more on file uploading =)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.