I have been using an upload script that has been working fine, but now it seems I am getting an error on almost all the files I upload. The script accually makes 3 images, thumbnail, midsized and orginal and uploads all. The error is as follows:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 5236 bytes) in /var/www/html/product-add-pictures-function.php on line 52

I understand that that 8mb and the files Im uploading are only around 500-800 KB.

Are there some tips that I can free up some PHP RAM on my server? I think 8mb is the max Im allowed. Could it be the way Im uploading the file, below is the upload script:

<?php 
//	upload_function.php
//
//	this page contains the upload function for upload_picture.php it also
//  dynamically creates the filenames and store the picture in the /pictures/ dir


session_start();
require 'page-admin.php';
require_once 'register.php';
loadAdminHeader("Add Product Pictures");

// Set small and large deminsionz
$smallwidth = 104;
$smallheight = 147;

$largeheight = 455;
$largewidth = 275;



// upload directory
//$uploaddir = '/home/virtual/site433/fst/var/www/html/dev/pictures/';   //this is my main image directory 
$uploaddir = '/var/www/html/pictures/';   //this is my main image directory 


// The file
$filename = $_FILES['userfile']['tmp_name'];

//explode the filename and rename it
$lastp = explode(".", $_FILES['userfile']['name']);


switch ( strtolower($lastp[1]) ) {

case "":
break;

case "jpg":



list($width_orig, $height_orig) = getimagesize($filename);

if ($largewidth && ($width_orig < $height_orig)) {
   $largewidth = ($largeheight / $height_orig) * $width_orig;
} else {
   $largeheight = ($largewidth / $width_orig) * $height_orig;
}


$image = imagecreatefromjpeg($filename);
// Resample
$image_p = imagecreatetruecolor($smallwidth, $smallheight);
$image_B = imagecreatetruecolor($largewidth, $largeheight);

//connect to the DB and get the num_rows for the filename
db_connect();
//$result1 = mysql_query("SELECT MAX(id) AS value FROM `cart_pictures`");
//num_rows1 = mysql_num_rows($result1);

$newVal = "SELECT MAX(id) AS value FROM `cart_pictures`";
$query32 = mysql_query($newVal) or die(mysql_error()); 
$result4 = mysql_fetch_array($query32);
//$ider = $result1['id'];
$ider = $result4['value'];
db_disconnect();

// set the filenames

$img1_name = $ider+2;
$img2_name = $img1_name."a";
$img1_test = $img1_name."BIG";

echo "<br><br>NEW ID IS: $img1_name<br><br>";

//testing
//copy big image
//$size = getimagesize("$filename");
//$image_C = imagecreatetruecolor($size[0],$size[1]);
//imagecopyresampled($image_C, $image, 0, 0, 0, 0, $width_orig, $height_orig, $width_orig, $height_orig);
//imagejpeg($image_C, $filename);
// Resample
$image_BIG = imagecreatetruecolor($width_orig, $height_orig);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_BIG, $image, 0, 0, 0, 0, $width_orig, $height_orig, $width_orig, $height_orig);
//copy($filename, "/home/virtual/site433/fst/var/www/html/dev/pictures/$img1_test.jpg");
copy($filename, "/var/www/html/pictures/$img1_test.jpg");


//makes bigger image
imagecopyresampled($image_B, $image, 0, 0, 0, 0, $largewidth, $largeheight, $width_orig, $height_orig);

// Output and save big picture
imagejpeg($image_B, $filename);

//copy big image
//copy($filename, "/home/virtual/site433/fst/var/www/html/pictures/$img1_name.jpg");
copy($filename, "/var/www/html/pictures/$img1_name.jpg");

//makes small thumb image
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $smallwidth, $smallheight, $width_orig, $height_orig);
imagejpeg($image_p, $filename);

$lastp[0] = $img2_name;

$_FILES['userfile']['name'] = $lastp[0].".".$lastp[1];

$uploadfile = $uploaddir . $_FILES['userfile']['name']; 

//move files and insert values into database  
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)); chmod("/var/www/html/pictures/$img2_name.jpg", 0644); db_connect(); $picUrl = "pictures/".$img1_name.".jpg"; $picUrla = "pictures/".$img2_name.".jpg"; //make sure varibles are post data $products = $_POST['products']; $txtTitle = $_POST['txtTitle']; //delete old value $sql = "UPDATE `cart_pictures` SET `delstatus` = 'inactive' WHERE `prod_id` = '$products';"; $query = mysql_query($sql) or die(mysql_error()); //add to sql $sql = "INSERT INTO `cart_pictures` ( `id` , `title` , `b_url` , `s_url` , `delstatus` , `prod_id` ) VALUES ('', '$txtTitle', '$picUrl', '$picUrla', 'active', '$products')"; $query = mysql_query($sql) or die(mysql_error());
db_disconnect();
print "Success! Your file was moved!<br><br>"; echo "<br><a href=\"$picUrl\" target=\"_blank\"><img src=\"$picUrla\" border=\"0\" alt=\"$title - click to enlarge\"><br>Click to enlarge</a>"; /*echo"<p><a href=\"javascript:showPic('$picUrl','click to close',$largewidth,$largeheight,'#919191','#25262c');\"> <img src=\"$picUrla\" width=$smallwidth height=$smallheight alt=\"\" border=0 vspace=5 hspace=5 style=\"filter:alpha(opacity=100)\" onmouseover=\"elFader(this,100,60,30)\" onmouseout=\"elFader(this,50,60,10)\"></a> </p>"; */ break; case "gif": echo"file is a gif"; /* checkStatus(); // getDeminsionz(); $image = imagecreatefromgif($filename); // Resample $image_p = imagecreate($smallwidth, $smallheight); $image_B = imagecreate($largewidth, $largeheight); getnewName(); // set the filenames $img1_name = $num_rows1+1; $img2_name = $img1_name."a"; //makes bigger image imagecopyresampled($image_B, $image, 0, 0, 0, 0, $largewidth, $largeheight, $width_orig, $height_orig); // Output and save big picture imagegif($image_B, $filename); copy($filename, "/home2/roundedv/public_html/ehl/pictures/$img1_name.gif"); //makes small thumb image imagecopyresampled($image_p, $image, 0, 0, 0, 0, $smallwidth, $smallheight, $width_orig, $height_orig); imagegif($image_p, $filename); //imagewbmp($image_p, $filename); $lastp[0] = $img2_name; $_FILES['userfile']['name'] = $lastp[0].".".$lastp[1]; $uploadfile = $uploaddir . $_FILES['userfile']['name']; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)); db_connect(); $picUrl = "pictures/".$img1_name.".gif"; $picUrla = "pictures/".$img2_name.".gif"; $id = $_SESSION['userid']; $sql = "INSERT INTO `ehl_pictures` (`pictureid`, `homeid`, `userid`, `url`, `title`, `default`) VALUES ('$img1_name', '$homes', '$id', '$picUrl', '$title', '$default')"; $query = mysql_query($sql) or die(mysql_error());
db_disconnect();
print "Sucess! Your file was moved!<br><br>"; //echo "<a href=\"$picUrl\" target=\"_blank\"><img src=\"$picUrla\" border=\"0\" alt=\"$title - click to enlarge\"><br>Click to enlarge</a>"; echo"<p><a href=\"javascript:showPic('$picUrl','click to close',$largewidth,$largeheight,'#919191','#25262c');\"> <img src=\"$picUrla\" width=$smallwidth height=$smallheight alt=\"\" border=0 vspace=5 hspace=5 style=\"filter:alpha(opacity=100)\" onmouseover=\"elFader(this,100,60,30)\" onmouseout=\"elFader(this,50,60,10)\"></a> </p>"; */ break; case "bmp": echo"file is a bmp"; // checkStatus(); break; case "png": echo"file is a png"; // checkStatus(); break; default: echo "Sorry this is an unacceptable file type"; unlink($filename); exit; } /* */ db_disconnect(); loadAdminFooter(); ?>

    is this the product-add-pictures-function.php page?

      yes it is, a temporary fix is that we limited the files to under 300kb, but i am interested if there is anything we can do to help free up some ram.

      thanks for the interest

        The reason that your memory is being exausted is bceause images are stored in the disk in compressed format and take up much more when they are read in through the GD library. Compiling your PHP engine with out the --enable-memory-limit option is one choice and editing php.ini to increase the memory limit is another.

        --
        Thin Image Upload
        Multiple image upload with client side resize and filtering.

          Write a Reply...