homepage Welcome to WebmasterWorld Guest from 54.205.188.59
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Image Upload - Resize using Linux
woldie




msg:1255583
 9:26 am on Jan 27, 2006 (gmt 0)

Hi,

I've got an image upload script, which resizes the image using the Linux image commands.

What baffles me is that if a upload an image, it works okay, and then if a upload another image, I get the following error:

Warning: unlink(): No such file or directory in /path/to/image on line 167

I can't figure out why its doing this?

Thanks again.

Code:

if ($REQUEST_METHOD == "POST")
{

/*== upload directory where the file will be stored
relative to where script is run ==*/

$uploaddir = "/path/to/images/_images";

/*== get file extension (fn at bottom of script) ==*/
/*== checks to see if image file, if not do not allow upload ==*/
$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";

/*== delete uploaded file ==*/
unlink($imgfile);
exit();
}

//-- RE-SIZING UPLOADED IMAGE

/*== only resize if the image is larger than 250 x 200 ==*/
$imgsize = GetImageSize($imgfile);

/*== check size 0=width, 1=height ==*/
if (($imgsize[0] > 250) ($imgsize[1] > 200))
{
#echo $imgsize[0].'<br>';
#echo $imgsize[1].'<br>';
/*== temp image file -- use "tempnam()" to generate the temp
file name. This is done so if multiple people access the
script at once they won't ruin each other's temp file ==*/
$tmpimg = tempnam("/tmp", "MKUP");

/*== RESIZE PROCESS
1. decompress jpeg image to pnm file (a raw image type)
2. scale pnm image
3. compress pnm file to jpeg image
==*/

/*== Step 1: djpeg decompresses jpeg to pnm ==*/
system("djpeg $imgfile >$tmpimg");

/*== Steps 2&3: scale image using pnmscale and then
pipe into cjpeg to output jpeg file ==*/
system("pnmscale -xy 250 200 $tmpimg cjpeg -smoo 10 -qual 50 >$imgfile");

/*== remove temp image ==*/
unlink($tmpimg);

}

/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";

/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($imgfile))
{

/*== move file to proper directory ==*/
if (!copy($imgfile,"$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
}

/*== delete the temporary uploaded file ==*/

// THIS IS WHERE IT FLAGS THE ERROR
unlink($imgfile);


print("<img src=\"path/to/image$final_filename\">");

/*== DO WHATEVER ELSE YOU WANT
SUCH AS INSERT DATA INTO A DATABASE ==*/

}
?>

<form action="<?=$SCRIPT_NAME;?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">

<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>

 

coopster




msg:1255584
 9:26 pm on Feb 2, 2006 (gmt 0)

Have you double checked the path and the file at this point? Dump the variable to the browser and exit the script right before the unlink() function in question then you can see.

woldie




msg:1255585
 9:48 am on Feb 3, 2006 (gmt 0)

Hi Coopster,

Thanks for the response.

Yeah I've dumped the variables, if you look at the code below, where its says '//-- RE-SIZING UPLOADED IMAGE'

I've echoed out the variables, in some cases it displays the tmp filename and actual file name i.e. file1.jpg. Yes I've also checked the path and thats fine.

So I'm at loss.

Thanks.

if ($REQUEST_METHOD == "POST")
{

/*== upload directory where the file will be stored
relative to where script is run ==*/

$uploaddir = "/path/to/images/_images";

/*== get file extension (fn at bottom of script) ==*/
/*== checks to see if image file, if not do not allow upload ==*/
$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";

/*== delete uploaded file ==*/
unlink($imgfile);
exit();
}

//-- RE-SIZING UPLOADED IMAGE

echo $imgfile_name.'<br>';
echo $imgfile.'<br>';

/*== only resize if the image is larger than 250 x 200 ==*/
$imgsize = GetImageSize($imgfile);

/*== check size 0=width, 1=height ==*/
if (($imgsize[0] > 250) ($imgsize[1] > 200))
{
#echo $imgsize[0].'<br>';
#echo $imgsize[1].'<br>';
/*== temp image file -- use "tempnam()" to generate the temp
file name. This is done so if multiple people access the
script at once they won't ruin each other's temp file ==*/
$tmpimg = tempnam("/tmp", "MKUP");

/*== RESIZE PROCESS
1. decompress jpeg image to pnm file (a raw image type)
2. scale pnm image
3. compress pnm file to jpeg image
==*/

/*== Step 1: djpeg decompresses jpeg to pnm ==*/
system("djpeg $imgfile >$tmpimg");

/*== Steps 2&3: scale image using pnmscale and then
pipe into cjpeg to output jpeg file ==*/
system("pnmscale -xy 250 200 $tmpimg cjpeg -smoo 10 -qual 50 >$imgfile");

/*== remove temp image ==*/
unlink($tmpimg);

}

/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";

/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($imgfile))
{

/*== move file to proper directory ==*/
if (!copy($imgfile,"$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
}

/*== delete the temporary uploaded file ==*/

// THIS IS WHERE IT FLAGS THE ERROR
unlink($imgfile);

print("<img src=\"path/to/image$final_filename\">");

/*== DO WHATEVER ELSE YOU WANT
SUCH AS INSERT DATA INTO A DATABASE ==*/

}
?>

<form action="<?=$SCRIPT_NAME;?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">

<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>

coopster




msg:1255586
 8:38 pm on Feb 4, 2006 (gmt 0)

Are you sure the file actually got uploaded on the attempt? I don't see any error checking. The PHP manual pages on Handling file uploads [php.net] explains the 'error' index in the $_FILES superglobal and the different codes are explained. I always make this a part of any file-uploading processing. It will tell you exactly where in the upload process that the upload failed and why.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved