adam291086 Posted December 11, 2007 Share Posted December 11, 2007 I have this upload script that uploads an image and resizes it. At the moment it keeps echoing out upload not successful. I have echoed out the information where the file is store temporarily and all is present and correct. Any ideas? <?php if ($_SERVER['REQUEST_METHOD'] == "POST") { /*== upload directory where the file will be stored relative to where script is run ==*/ $dir = dirname(__FILE__)."/pictures/"; $path= $dir; /*== 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)) { $tmpimg = tempnam("/tmp", "MKUP"); /*== 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); /*== do extra security check to prevent malicious abuse==*/ if (is_uploaded_file($imgfile)) { /*== move file to proper directory ==*/ if(move_uploaded_file($final_filename, $path)) { echo "Magazine Updated!"; } else { echo "Upload not Successful"; unlink(@$imgfile); } } } ?> Function <?php /*== FUNCTIONS ==*/ function getFileExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 11, 2007 Share Posted December 11, 2007 If I remember correct I think the move_upload_file will only work on files that are uploaded as I can see in the script you have modified it somehow so it will not execute. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 ok, i have change the script to use copy instead <?php if ($_SERVER['REQUEST_METHOD'] == "POST") { /*== upload directory where the file will be stored relative to where script is run ==*/ $dir = dirname(__FILE__)."/pictures/"; $path= $dir; /*== 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)) { $tmpimg = tempnam("/tmp", "MKUP"); /*== 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")) { echo "Upload not Successful"; unlink(@$imgfile); } else { echo "Magazine Updated!"; } } } I am still getting the same problem Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 forgot to change a variable. Thanks. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 ok, all of a sudden my upload script has stopped working. The page loads with no error. I echo out the variable and they are present. But nothing is upload to my directory and the success message isn't displayed. Anyone know why? <?php if ($_SERVER['REQUEST_METHOD'] == "POST") { /*== upload directory where the file will be stored relative to where script is run ==*/ $dir = dirname(__FILE__)."/pictures/"; $path= $dir; /*== 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)) { $tmpimg = tempnam("/tmp", "MKUP"); /*== 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 = $path . "/$final_filename"; /*== do extra security check to prevent malicious abuse==*/ if (is_uploaded_file($imgfile)) { /*== move file to proper directory ==*/ if (!copy($imgfile,"$newfile")) { echo "Upload not Successful"; unlink(@$imgfile); } else { echo "Magazine Updated!"; } } } ?> <?php /*== FUNCTIONS ==*/ function getFileExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } ?> Quote Link to comment Share on other sites More sharing options...
Lumio Posted December 11, 2007 Share Posted December 11, 2007 maybe you directory is not writeable. Try error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 still getting nothing. No errors or anything. ??? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 now i am getting the error Warning: unlink() [function.unlink]: No such file or directory in /homepages/12/d214897219/htdocs/adam/cycling/admin/upload/uploadimage.php on line 88 this is line 88 unlink($imgfile); any ideas? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 ok sort of got it work. if ($pext ! == 'jpg' || $pext !== 'jpeg'){ I want to say when $pect doesn't = jpg but at the moment i keep getting the error Parse error: parse error, unexpected '!' in /homepages/12/d214897219/htdocs/adam/cycling/admin/upload/uploadimage.php on line 15 line 15 being the code above Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2007 Share Posted December 12, 2007 your code should be if ($pext != 'jpg' || $pext != 'jpeg'){ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.