Re: [PHP] Upload and resize file
While I'm not 100% sure, I'd say something's wrong with this line: system(pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 $imgfile); I would say that Linux is not writing the output to the location stored in $imgfile, and thus there is no file there to delete. However, I can not really be sure. Can someone support or deny this claim? - Craige On 8/22/07, Beauford [EMAIL PROTECTED] wrote: I downloaded this 'upload and resize image' script, and since I have no idea what I am looking at as this is something I have never done, could someone have a look and see what the problem might be. I've been searching around but haven't come across anything that makes any sense yet. When a picture needs to be resized I am getting the following error. If it is the same size or under I don't get this error. Warning: unlink() [function.unlink]: No such file or directory in /usr/local/apache/htdocs/website/upload.php on line 92 The full code is below. Thanks -- html head titleweb.blazonry : PHP : Upload and Resize an Image/title ?php if ($_SERVER['REQUEST_METHOD'] == POST) { /* SUBMITTED INFORMATION - use what you need * temporary filename (pointer): $imgfile * original filename : $imgfile_name * size of uploaded file : $imgfile_size * mime-type of uploaded file : $imgfile_type */ /*== upload directory where the file will be stored relative to where script is run ==*/ $uploaddir = 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 h1ERROR/h1Image Extension Unknown.br; print pPlease upload only a JPEG image with the extension .jpg or .jpeg ONLYbrbr; 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)) { /*== 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 23: 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 ==*/ unlink($imgfile); print(img src=\images\\$final_filename\); /*== DO WHATEVER ELSE YOU WANT SUCH AS INSERT DATA INTO A DATABASE ==*/ } ? /head body bgcolor=#FF h2Upload and Resize an Image/h2 form action=?php echo $_SERVER['PHP_SELF']; ? method=POST enctype=multipart/form-data input type=hidden name=MAX_FILE_SIZE value=5 pUpload Image: input type=file name=imgfilebr font size=1Click browse to upload a local file/fontbr br input type=submit value=Upload Image /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; } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Upload and resize file
Beauford wrote: I downloaded this 'upload and resize image' script, and since I have no idea what I am looking at as this is something I have never done, could someone have a look and see what the problem might be. I've been searching around but haven't come across anything that makes any sense yet. When a picture needs to be resized I am getting the following error. If it is the same size or under I don't get this error. Warning: unlink() [function.unlink]: No such file or directory in /usr/local/apache/htdocs/website/upload.php on line 92 I see at least 3 places where unlink() is called. Which one is at line 92? Knowing that will go along way toward understanding where the script has gotten to and what else might be going wrong. You should place some print statements in the script just before each unlink() call. Something like: print 'first one'; print $tmpimg'; unlink($tmpimg); ... print 'second one'; print $tmpimg'; unlink($tmpimg); etc. This way you'll be able to at least see what is in the var $tmpimg. brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Upload and resize file
On Thursday 23 August 2007 03:41, Beauford wrote: I downloaded this 'upload and resize image' script, and since I have no idea what I am looking at as this is something I have never done, could someone have a look and see what the problem might be. I've been searching around but haven't come across anything that makes any sense yet. When a picture needs to be resized I am getting the following error. If it is the same size or under I don't get this error. Warning: unlink() [function.unlink]: No such file or directory in /usr/local/apache/htdocs/website/upload.php on line 92 This full code of yours should be no more than 7 lines. However, delete line 92, it's not needed to make the script actually upload stuff. You should have a file like php12312 file name or similar at the default upload path defined in php.ini. This file is the $_file['tmp_name'] within the $files array, so while at it; don't make use of a $formname for everything. If you make use of the $_files['array'] stuff the code would be so much more manageable within the linux os. If the temp file does not occur; you got the netpbm and djpeg installed? Not sure about making this sort of things work under a windows system, but on linux its a sundaysprawl The full code is below. Thanks -- html head titleweb.blazonry : PHP : Upload and Resize an Image/title ?php if ($_SERVER['REQUEST_METHOD'] == POST) { /* SUBMITTED INFORMATION - use what you need * temporary filename (pointer): $imgfile * original filename : $imgfile_name * size of uploaded file : $imgfile_size * mime-type of uploaded file : $imgfile_type */ /*== upload directory where the file will be stored relative to where script is run ==*/ $uploaddir = 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 h1ERROR/h1Image Extension Unknown.br; print pPlease upload only a JPEG image with the extension .jpg or .jpeg ONLYbrbr; 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)) { /*== 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 23: 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 ==*/ unlink($imgfile); print(img src=\images\\$final_filename\); /*== DO WHATEVER ELSE YOU WANT SUCH AS INSERT DATA INTO A DATABASE ==*/ } ? /head body bgcolor=#FF h2Upload and Resize an Image/h2 form action=?php echo $_SERVER['PHP_SELF']; ? method=POST enctype=multipart/form-data input type=hidden name=MAX_FILE_SIZE value=5 pUpload Image: input type=file name=imgfilebr font size=1Click browse to upload a local file/fontbr br input type=submit value=Upload Image /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;
[PHP] Upload and resize file
I downloaded this 'upload and resize image' script, and since I have no idea what I am looking at as this is something I have never done, could someone have a look and see what the problem might be. I've been searching around but haven't come across anything that makes any sense yet. When a picture needs to be resized I am getting the following error. If it is the same size or under I don't get this error. Warning: unlink() [function.unlink]: No such file or directory in /usr/local/apache/htdocs/website/upload.php on line 92 The full code is below. Thanks -- html head titleweb.blazonry : PHP : Upload and Resize an Image/title ?php if ($_SERVER['REQUEST_METHOD'] == POST) { /* SUBMITTED INFORMATION - use what you need * temporary filename (pointer): $imgfile * original filename : $imgfile_name * size of uploaded file : $imgfile_size * mime-type of uploaded file : $imgfile_type */ /*== upload directory where the file will be stored relative to where script is run ==*/ $uploaddir = 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 h1ERROR/h1Image Extension Unknown.br; print pPlease upload only a JPEG image with the extension .jpg or .jpeg ONLYbrbr; 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)) { /*== 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 23: 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 ==*/ unlink($imgfile); print(img src=\images\\$final_filename\); /*== DO WHATEVER ELSE YOU WANT SUCH AS INSERT DATA INTO A DATABASE ==*/ } ? /head body bgcolor=#FF h2Upload and Resize an Image/h2 form action=?php echo $_SERVER['PHP_SELF']; ? method=POST enctype=multipart/form-data input type=hidden name=MAX_FILE_SIZE value=5 pUpload Image: input type=file name=imgfilebr font size=1Click browse to upload a local file/fontbr br input type=submit value=Upload Image /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; } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php