Link to home
Start Free TrialLog in
Avatar of uknetweb
uknetweb

asked on

Why does this JPEG break my file upload script?

Recently my file upload script has been causing a few problems;

I have narrowed it down to a number of images that seem to cause the problem.

These particular images will somehow prevent the $_FILES array, and other variables in my form from being posted.

Here is my form
<html>
<head>
<title>Image Upload</title>
<script src="funcs.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body onload="self.resizeTo(400, 200)">
<form enctype="multipart/form-data" name="theform" action="./upload.php" method="POST">
<table class="tbl_emailsys">
  <tr>
    <td class="head" colspan="2">Select A File To Upload:</td>
  </tr>
  <tr>    
    <td>
    <input type="hidden" name="event" value="upload">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"><input type="file" name="uploadfile" style="border: 1px solid #777777; width: 250px;">    </td>
  </tr>
  <tr>
    <td><input class="button" type="submit" value="Upload">&nbsp;<input  class="button" type="button" onclick="self.close()" value="&nbsp;&nbsp;Close&nbsp;&nbsp;"></td>
            <input type="hidden" name="img" value="image1">
  </tr>
  <tr>
    <td style="vertical-align:top;">Resize ? <input type="hidden" name="resize" value="false"><input type="checkbox" name="resize" value="true">
    <span style="font-size: smaller;vertical-align:top;"> Tick the box to resize the image when you upload</span></td>
  </tr>
  <tr>
            <td colspan="2" style="font-size: smaller">(Images must be *.gif or *.jpg and smaller than 1MB)</td>
  </tr>
  <tr>
        <td>
        </td>
  </tr>
</table>
</form>
<div class="mgrdebug">Dumping Errors<br>error mask is 1023<br></div>
</body>
</html>

I have 2 images that are similar visually, but slightly different in terms of dimensions and file size.

Image 1 can be found at http://www.uknetweb.co.uk/images/test/image1.jpg
It is 1648 * 2464 pixels and is approx 266kb in size
Image 2 can be found at http://www.uknetweb.co.uk/images/test/image2.jpg
it is 1632 * 2440 pixels and is approx 592kb in size

The post_max_size setting for php is 8M and the memory limit was raised to 20M

I can upload both files fine if I don't tick the resize box.

If I tick the resize box in my form then try to upload file 1.  It  does not work - the error log for the server shows:
"Allowed memory size of 20971520 bytes exhausted (tried to allocate 138 bytes)"
And my debugging output shows that the $_FILES array is empty, with the "MAX_FILE_SIZE", "resize" and "event" form fields not being posted.

If I tick the resize box in my form then try to upload file 2.  It  does work.

My script does not even get as far as trying to deal with the upload of the file as the "event" field triggers this.
Here is the php.

<?php
session_start();
include_once 'UploadImage.php.inc';

$event = '';

/*
      * @param $file the file to be uploaded
      * @param $size the maximum file size allowed in Mb
      * @param $dir the path to where the file will be moved
      * @param $type the extension type of the file allowed
      * @param $num - 1 or 2 - specifies the "target" image in the parent window.
*/
$file = 'uploadfile';
$size = 1;

global $gImgUploadDir;
global $gAllowedImages;

$img =      isset($_REQUEST['img']) ? $_REQUEST['img'] : '';

$rsz = TRUE;
/*
Some images break the upload script completely...
*/
$rsz = isset($_REQUEST['resize']) ? $_REQUEST['resize'] : FALSE;
if("true" == $rsz) {
    $rsz = TRUE;
} else {
    $rsz = FALSE;  
}

trigger_error("image is " .$img ." resize is " .var_export($rsz, TRUE) ." it was " .var_export($_REQUEST['resize'], TRUE), E_USER_NOTICE);


$upload = new UploadImage($file, $size, $gImgUploadDir, $gAllowedImages, $img, 0);

trigger_error('$_REQUEST ' .var_export($_REQUEST, TRUE));
//trigger_error('$_FILES ' .var_export($_FILES, TRUE));

if(isset($_REQUEST['event'])) {
  $event = $_REQUEST['event'];
  if ($event == "upload") {
        $upload->setResize($rsz);
      $upload->setResizeMode(IMAGE_WIDTH_FIT);
      if($upload->do_upload()) {
          //echo "Upload";
          //The do_upload function of the UploadImage class will resize images.
          
          $event = "uploaded";            
    } else {
      $event = "error";            
    }
  }
}

/*
  Output below here is HTML & PHP for the 3 $event states '' 'error', 'uploaded'
  The default is to show the above form.
*/

Any ideas??
Avatar of tkalchev
tkalchev
Flag of Germany image

<input type="hidden" name="resize" value="false">
and
<input type="checkbox" name="resize" value="true">


Both has the same name "resize", maybe this is the problem
Also check upload_max_filesize in your php.ini
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of uknetweb
uknetweb

ASKER

The image in memory was taking up about 16MB.  

Then my script(s) on top of that were taking up 4MB (some of this could be GD library as I was trying to resize the images)

The first image must have taken PHP just over the limit, with the second being just under...

Thanks for your help.

Glen