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