Hi Everyone,

Sorry about posting one image upload question after the other, but (of course) mine is a bit different. I have a script that I want to use to upload an image, rename it so it won't write over another, resize it so that it's 500px wide, and copy to a folder. Here's what I have:

<?php
if ($REQUEST_METHOD == "POST") 
{
    $uploaddir = "../images/photos/";
    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != "jpg")  && ($pext != "jpeg"))
    {
        print "<h1>ERROR</h1>Image Extension Unknown.<br>";
        print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
        print "The file you uploaded had the following extension: $pext</p>\n";

    unlink($imgfile);
    exit();
}
$imgsize = GetImageSize($imgfile);
if (($imgsize[0] > 500) || ($imgsize[1] > 500)) 
{
    $tmpimg = tempnam("/tmp", "MKUP");
    system("djpeg $imgfile >$tmpimg");
    system("pnmscale -xy 500 500 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
    unlink($tmpimg);

}
$ext = substr(strrchr($imgfile_name, "."), 1);
$final_filename = md5(rand() * time());
$newfile = $uploaddir . $final_filename . '.' . $ext;

if (is_uploaded_file($imgfile))
{
   if (!copy($imgfile,"$newfile")) 
   {
      print "Error Uploading File.";
      exit();
   }
 }

unlink($imgfile);

print("<img src=\"../images/photos/$final_filename.$ext\">");
}
?>
    <form action="<?=$SCRIPT_NAME; ?>" method="POST" enctype="multipart/form-data">
      <p>
        <input type="hidden" name="MAX_FILE_SIZE" value="20000000">
      Upload Image: 
  <input type="file" name="imgfile">
      <br>
      <br>
      <input type="submit" value="Upload Image">
      </span>
</form>
    </body>
</html>

<?php

/*== FUNCTIONS ==*/

function getFileExtension($str) {

    $i = strrpos($str,".");
    if (!$i) { return ""; }

    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);

    return $ext;

}
?>

It does everything I'm asking of it, except...the file when copied is 0kb. Anyone see anything out of place?

azo

    The first thing I'd do is make a permanent copy of the original, temp, and final files so you can check them see where the file loses it's contents. I suspect that the pnmscale function is causing the problem, but that's just a guess. But if it were me, I'd start my troubleshooting there.

      You're right, that's what is causing it. I comment it out and it works fine. Now my only problem is that I want it to resize the images, but no matter what I try it only uploads a 0kb file. Any next steps?

      Azo

        What is the reason for using command line tools to resize the image instead of one of PHP's image extensions?

        Also, if I'm reading your code right (I'm having to guess because otherwise there's no explanation about where $imgfile or $imgfile_name come from and no assurance they're correct or used correctly when they appear), you're working with register_globals turned on; I strongly recommend you turn it off and use the $_FILES array to retrieve information about uploaded files.

        Oh, and getimagesize() can identify the type of the uploaded file more reliably than just trusting the filename extension.

          Write a Reply...