Solved

Getting Fatal error when trying to upload

Posted on 2004-08-20
19
455 Views
Last Modified: 2012-08-14
I get this error when I try uploading large images (around 1-2MB). I also noticed something weird -> I can upload a 1.3MB file with dimensions of 2272 x 1704 but if I upload an 800K file with dimensions of 3000 x 2000, I get a similar error if not the same.

----------------------------------------------------------------------------------------------------------------
Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 12288 bytes) in /var/www/html/Hosting/ourwedsite/includes/upload.php on line 34
----------------------------------------------------------------------------------------------------------------

Does anyone know why?
0
Comment
Question by:bloodtrain
  • 10
  • 6
  • 3
19 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 11852694
you could increase the memory limit in your php.ini file

find this section

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60      ; Maximum amount of time each script may spend parsing request data
memory_limit = 8M      ; Maximum amount of memory a script may consume (8MB)

and increase memory_limit

however i suspect that there may be a bug in your script, you say that you are only trying to upload a 800k file but
over 24meg of memory is being used.
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 100 total points
ID: 11852716
incidently if you do need to increase the memory limit but cant access your php.ini file use the ini_set function (http://www.php.net/manual/en/function.ini-set.php)

eg. (at top of script)

ini_set("memory_limit","50M");
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11852764
well, my php.ini is already set to:

max_execution_time = 30;
max_input_time = 60;
memory_limit = 24M;

I had set it to 24M already because I had the same problem before and changing it to 24M worked but I guess I never uploaded a big enough file until now.

Does the dimensions of an image file play a role? Does it need more memory?
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11852775
you post to quick.. hehe.

should I set it to 50M in my PHP.INI ? How much is too much?
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11852800
uploading a 800k image should not exhast 24meg of memory is the bottom line.

with regards to "How much is too much?" it comes down to how much have you got and how many users will be uploading files at anyone time. BUT as i said its using much more memory then it should so id like to have a look at your script if possible.
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11852825
Sure, here's the whole page including html just in case you wanted to check something. (sorry if it's messy)


--------------------------------------------------------------------------------

<?php
include("../includes/main.php");
mysql_connect ($db_hostname, $db_username, $db_password) or die ('Server is busy. Please try again later. Thank you.');
mysql_select_db($db_name) or die ('Cannot connect to database. Please try again later. Thank you.');

/****************************************
 Check if user is Signed In
****************************************/
$var_id = 0;
$var_password = "";

if(isset($_COOKIE['ows_userID']) != ""){ $var_id = $_COOKIE['ows_userID']; }
if(isset($_COOKIE['ows_password']) != ""){ $var_password = $_COOKIE['ows_password']; }
$query = "SELECT * FROM tbl_uploaders WHERE uploader_id = $var_id AND uploader_password = '$var_password'";
$rs = mysql_query($query) or die("There has been an expected error. Please click your browser's Back button to continue.");
$rs_rows = mysql_num_rows($rs);
if($rs_rows == 0){
      header("Location: /signin.php");
}
/***************************************/

if(isset($_GET['eid']) != ""){ $var_eventID = $_GET['eid']; }

$query = "SELECT * FROM tbl_events INNER JOIN tbl_eventlink ON tbl_events.event_id = tbl_eventlink.el_eventID WHERE tbl_eventlink.el_customerID = $couple_id AND tbl_eventlink.el_hidden = 0 ORDER BY event_sortorder";
$rsEvents = mysql_query($query) or die("The query failed. ".mysql_error());
$event_of_rows = mysql_num_rows($rsEvents);

$ul_errornumber = 0;
$ul_errormessage = "";

if(isset($_REQUEST["txt_firstTime"]) == "no"){
      if($HTTP_POST_FILES['imagefile']['name'] != "")  #*** if file field is empty
      {      
            $src_img = imagecreatefromjpeg($_FILES['imagefile']['tmp_name']);
            $src_w = ImageSX($src_img);
            $src_h = ImageSY($src_img);
            $src_filesize = $_FILES['imagefile']['size'];
            $var_date = date("Y-m-d H:i:s");
            
            if(src_filesize > 2048000)
            {  
                  $ul_errornumber = 1;
                  $ul_errormessage = "The file you submitted is too large. The maximum file size is 2MB (2048000 bytes).";
            }
            else if(($_FILES['imagefile']['type'] != "image/pjpeg") && ($_FILES['imagefile']['type'] != "image/jpeg"))
            {
                  $ul_errornumber = 1;
                  $ul_errormessage = "The file you submitted is not a JPG";
            }

            if($ul_errornumber == 0){

            $result = mysql_query("INSERT INTO tbl_images (image_customerID,image_width,image_height,image_filesize,image_date,image_uploaderID,image_eventID) VALUES ($couple_id,$src_w,$src_h,$src_filesize,'$var_date',$var_id,$var_eventID)");
            $var_imageID = mysql_insert_id();

                  if(!(copy($_FILES['imagefile']['tmp_name'], "pictures/" . $var_imageID . ".jpg")))
                  {
                        $ul_errornumber = 1;
                        $ul_errormessage = "Unexpected error occured. Please try again.";
                        $result = mysql_query("DELETE FROM tbl_images WHERE image_id = $var_imageID");
                  }
                  else
                  {
                        $dst_w = 380;
                        $dst_h = 285;
                        $dst_img = imagecreatetruecolor($dst_w,$dst_h);
                        imageCopyResampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
                        imagejpeg($dst_img, "pictures/" . $var_imageID . "_p.jpg", 50);
                        
                        $dst_w = 90;
                        $dst_h = 68;
                        $dst_img = imagecreatetruecolor($dst_w,$dst_h);
                        imageCopyResampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
                        imagejpeg($dst_img, "pictures/" . $var_imageID . "_tn.jpg", 50);

                        ImageDestroy($src_img);
                        header("Location: gallery.php?eid=$var_eventID");
                  }
            }

      } #*** If file field is not empty
      else
      {  
            $ul_errornumber = 1;
            $ul_errormessage = "Please select an image to upload by clicking Browse.";
      }
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php Print $main_title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="/includes/style.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">
<!--
function loadImage(imageID) {
      document.frm.txt_print.value = imageID;
      document.frm.image_preview.src = "pictures/"+imageID+"_p.jpg";
      document.frm.txt_date.value = document.thumbnails.tn_array[imageID-1][2];
      document.frm.txt_uploader.value = document.thumbnails.tn_array[imageID-1][1];
      document.frm.txt_width.value = document.thumbnails.tn_array[imageID-1][3];
      document.frm.txt_height.value = document.thumbnails.tn_array[imageID-1][4];
}

function print_image(myid){
      if(myid == ""){
            alert("No image was selected. Please click on a thumbnail.");
      }
      else
      {
            window.open('pictures/'+myid+'.jpg','printing');
      }
}
//-->
</script>
</head>

<body class="main">
<form action="upload.php?eid=<?php Print $var_eventID; ?>" method="post" enctype="multipart/form-data" name="frm">
  <br>
<table width="744" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="46"><img src="/images/shared/rtable_top_1.jpg" width="46" height="46"></td>
    <td colspan="2"><img src="/images/shared/rtable_top_2.jpg" width="652" height="46"></td>
    <td width="46"><img src="/images/shared/rtable_top_3.jpg" width="46" height="46"></td>
  </tr>
  <tr>
    <td><img src="/images/shared/rtable_left_1.jpg" width="46" height="55"></td>
      <td width="260" valign="top" bgcolor="FFF8FE" class="headerLogo"><a href="http://www.ourwedsite.net"><img src="/images/shared/rtable_logo.gif" width="248" height="51" border="0"></a></td>
      <td width="403" align="right" valign="bottom" bgcolor="FFF8FE" class="language">
        <?php include("../includes/top_menu.php"); ?>
      </td>
    <td><img src="/images/shared/rtable_right_1.jpg" width="46" height="55"></td>
  </tr>
  <tr>
    <td valign="top" background="/images/shared/rtable_left_1.jpg">&nbsp;</td>
    <td colspan="2" valign="top" bgcolor="FFF8FE" class="rtable_mainBG"><img src="/images/spacer.gif" width="652" height="2"><br><img src="/<?php Print $couple_name; ?>/banner.jpg" width="652" height="150"><br><img src="/images/shared/banner_bottom.gif" width="652" height="5"><br><table width="652" height="450" border="0" cellpadding="0" cellspacing="0">
            <tr>
                  <td width="100" valign="top" class="column_1">
                  <?php include("menu.php"); ?>
                  </td>
                  <td width="552" valign="top" class="column_2"><span class="pageTitle">Upload Image</span><br>
              <br>
              <br>
              <b><u>STEP ONE</u></b><br>
              <br>
              Select a photo album to upload image in.<br>
              <br>
              <table width="400" border="0" cellspacing="0" cellpadding="0">
                  <?php
                  mysql_data_seek($rsEvents, 0);
                  while($row=mysql_fetch_array($rsEvents)){
                  ?>
                <tr>
                  <td width="25" valign="middle" class="text1">
                              <input type="radio" name="rad_album" value="<?php Print $row["event_id"]; ?>" <?php if($var_eventID == $row["event_id"]){ Print " checked";} ?>>
                  </td>
                  <td width="375" valign="middle" class="text1"><?php Print $row["event_name_e"]; ?><?php if($row["el_locked"] == 1){ Print " (<span style='color: #000000'>Locked</span>)"; }?></td>
                </tr>
                  <?php } ?>
              </table>
              <br>
              <br>
              <b><u>STEP TWO</u></b><br>
              <br>
              Select an image by clicking on Browse and locating it on your computer.<br>
              <br>
              <input name="imagefile" type="file" class="textbox" id="imagefile" style="width: 400px">
              <br>
              <br>
              <br>
              <b><u>STEP THREE</u></b><br> <br>
                  <b>Guidelines</b><br>
                  <br>
                  1. You can only upload .JPG/.JPEG images.<br>
                  2. No images larger than 2 megabytes will be accepted.<br>
                  3. Avoid uploading images that are very similar.<br>
              4. Avoid uploading blurry images.<br>
              <br>
              <?php if($ul_errornumber > 0){ ?>
              <span style="color: #C00101"><?php Print "Error: " . $ul_errormessage . "<br><br>";?></span>
              <?php } ?>
              <br>
              <input name="btn_upload" type="button" id="btn_upload" value="Upload" class="form_buttons" onClick="javascript:document.frm.submit();">
              <input name="btn_cancel" type="button" id="btn_cancel" value="Cancel" class="form_buttons" onClick="javascript:window.location.href='gallery.php?eid=<?php Print $var_eventID;?>'">
              <input name="txt_firstTime" type="hidden" id="txt_firstTime" value="no">
            </td>
        </tr>
      </table></td>
    <td valign="top" background="/images/shared/rtable_right_1.jpg">&nbsp;</td>
  </tr>
  <tr>
    <td><img src="/images/shared/rtable_bottom_1.jpg" width="46" height="46"></td>
    <td colspan="2" valign="top" background="/images/shared/rtable_bottom_2.jpg" class="copyright"><?php Print $main_footer; ?></td>
    <td><img src="/images/shared/rtable_bottom_3.jpg" width="46" height="46"></td>
  </tr>
</table>
</form>
</body>
</html>
<?php
mysql_close();
?>
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11852887
I took a look again at my php.ini and noticed that

post_max_size = 8M

Is that fine? Should it be more or has nothing to do with my problem?

Thanks
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11852910
forgot to mention that

upload_max_filesize = 4M

Either way, I max the limit to 2MB in my script.

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11852928
dont worry about that, not the problem i dont think.

i have just had a very quick look at your code and i see that you are using imagecreatefromjpeg however you do not appear to be destroying the image after which means its staying in the memory.

Once you have finished with the image add image_destroy(); to clear it out of the memory, more info in the manual here: http://www.php.net/manual/en/function.imagedestroy.php

If that doesnt fix it let me know and il have a more detailed look.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Author Comment

by:bloodtrain
ID: 11852973
I thought I was destroying the image. I have it in there before I redirect to another page.

$dst_w = 380;
$dst_h = 285;
$dst_img = imagecreatetruecolor($dst_w,$dst_h);
imageCopyResampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
imagejpeg($dst_img, "pictures/" . $var_imageID . "_p.jpg", 50);
                   
$dst_w = 90;
$dst_h = 68;
$dst_img = imagecreatetruecolor($dst_w,$dst_h);
imageCopyResampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
imagejpeg($dst_img, "pictures/" . $var_imageID . "_tn.jpg", 50);

ImageDestroy($src_img);    <----------------------------------------
header("Location: gallery.php?eid=$var_eventID");
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11853034
well, I've tried a few things and here is what I tried last, but no change:


                        $dst_w = 380;
                        $dst_h = 285;
                        $dst_img = imagecreatetruecolor($dst_w,$dst_h);
                        imageCopyResampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
                        imagejpeg($dst_img, "pictures/" . $var_imageID . "_p.jpg", 50);
                        imagedestroy($dst_img); <-----------------------------------------------

                        
                        $dst_w = 90;
                        $dst_h = 68;
                        $dst_img = imagecreatetruecolor($dst_w,$dst_h);
                        imageCopyResampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
                        imagejpeg($dst_img, "pictures/" . $var_imageID . "_tn.jpg", 50);
                        imagedestroy($dst_img); <-----------------------------------------------

                        imagedestroy($src_img); <-----------------------------------------------
                        header("Location: gallery.php?eid=$var_eventID");


0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11853054
oh yes missed that, i believe however you will need to also do it for $dst_img otherwise the memory allocated to its creation will not be freed.

Just got email notif as i was about to post the above, ok il have another look through your code and see if i can spot anything
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11853155
After having another look through your script i cannot see any obvious reasons for the error. Exceeding the memory limit is usually caused by one of two things, not deleting images after creation with the GD or a non escaping loop that runs for infinity.

You can increase the memory limit as a temporary solution but obviously the issue still needs to be correctly addressed, im going to have another look through your script just to check i havent missed anything but it would be worth while to wait for the opinion of someone that has more experience with using the GD library as that seems to be the likely cause here.
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11853196
I appreciate you helping me..

just wondering:
Right now, I create $dst_img twice: one for the preview image(380x285) and one for the thumbnail(90x68).  Is there a way to just resize the preview image 90 x 68 to save memory?

0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 11856205
I think that when an image is too large, and GD starts proccessing it, the uncompressed JPEG (when it virtually becomes a bitmap for GD) can take up a lot of space.
Think that it needs to allocate 18,000,000 bytes (18MB) just for working with 3000x2000x24bit image.
I don't know how it works, maybe it needs to have another cache with the same size, so obviously large images can use all the memory.

Increase the memory, or/and add a limit for image size...

It's not about the "weight" of the JPEG file. It's about the size (width X hight)...
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 11856225
Oh, and another thing:

You had the error on line 34, which is:
          $src_img = imagecreatefromjpeg($_FILES['imagefile']['tmp_name']);

I think it uses or tries to use two images with at least 18MB of data. One is the openned JPEG, and the other is the destrination bitmap to which it is putting the JPEG after extracting it.
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11857251
Nomaed:

I believe you are right about the dimensions and not the weight. But I don't understand what you mean about the error on line 34. Do you think you can provide me with the correct syntax?

Thanks.
0
 
LVL 3

Expert Comment

by:Boris Aranovich
ID: 11858252
The syntax is correct, as much as I know.
The thing is, that this function, imagecreatefromjpeg(), is returning this error, because when it runs, it uses too much memory, something which the PHP does not allow according to the php.ini
It works fine on smaller files because it does not reach the limit, but on the big files, it can't operate and return this fatal error.

Try setting the memory limit higher as someone said,
either in php.ini:
memory_limit = 64M
or by:
ini_set('memory_limit', '64M');
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11863837
well, I raised the memory limit for now so that it will work. Not the solution I was hoping for so now, I guess,  I gotta add some more RAM to my server.

Thanks Diablo84 for your help and everyone else as well.
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

13 Experts available now in Live!

Get 1:1 Help Now