textbox Posted May 22, 2007 Share Posted May 22, 2007 Hello again, this script works 'ok' It uploads fine, but then whenever I go to the page where the script sits, it deletes my current image and replaces it in the db as a period stop. Any ideas?! Also, as an add on, id like to create 3 different sized images from this script, can this be done?! If so, any help as to where id start would be great. <?php include "../include/session.php"; session_start(); header("Cache-control: private"); if (!$_SESSION['username']) { print "You're not logged in!"; include("index.php"); exit(); } include "../global/db.php"; //set the username for testing $username = $_SESSION['username']; ######################################## //load the config file include("imageconfig.php"); //if the for has submittedd if (isset($_POST['upForm'])){ $file_type = $_FILES['imgfile']['type']; $file_name = $_FILES['imgfile']['name']; $file_size = $_FILES['imgfile']['size']; $file_tmp = $_FILES['imgfile']['tmp_name']; //check if you have selected a file. if(!is_uploaded_file($file_tmp)){ echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>"; exit(); //exit the script and don't do anything else. } //check file extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "Wrong file extension. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>"; exit(); } //get the file extension. $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); //get the new width variable. $ThumbWidth = $img_thumb_width; //keep image type if($file_size){ if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); } //list width and height and keep height ratio. list($width, $height) = getimagesize($file_tmp); $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio; }else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio; } //function for resize image. if (function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth,$newheight); }else{ die("Error: Please make sure you have GD library ver 2+"); } imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //save image ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext"); ImageDestroy ($resized_img); ImageDestroy ($new_img); //print message echo "<br>Image Thumb: <a href=\"$path_thumbs/$rand_name.$file_ext\">$path_thumbs/$rand_name.$file_ext</a>"; } //upload the big image move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext"); echo "<br>Image Big: <a href=\"$path_big/$rand_name.$file_ext\">$path_big/$rand_name.$file_ext</a>"; echo "<br><br>--<a href=\"$_SERVER[php_SELF]\">back</a>"; }else{ //if the form hasn't been submitted. //print the form echo "<br><h3>:: Browse an Image to Upload:</h3>\n <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[php_SELF]\">\n <input type=\"file\" name=\"imgfile\"> <img src='' name='img_vv' width='0'><br>\n Image width will resize to <b>$img_thumb_width</b> with height ratio. <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n </form> "; } //enter the information into the correct table <br> $query="UPDATE images SET imagelocation='$path_big/$rand_name.$file_ext', thumblocation='$path_thumbs/$rand_name.$file_ext' WHERE username = '$username'"; mysql_query($query); //echo "Image Updated, please refresh!"; ?> Thanks Nick Quote Link to comment Share on other sites More sharing options...
textbox Posted May 22, 2007 Author Share Posted May 22, 2007 Is it because of the action of the form?! Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 What is wrong with your script? Quote Link to comment Share on other sites More sharing options...
textbox Posted May 23, 2007 Author Share Posted May 23, 2007 It uploads fine, but then whenever I go to the page where the script sits, it deletes my current image and replaces it in the db as a period stop. Any ideas?! Also, as an add on, id like to create 3 different sized images from this script, can this be done?! If so, any help as to where id start would be great. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 Does this script resize and to what dimension? Quote Link to comment Share on other sites More sharing options...
textbox Posted May 23, 2007 Author Share Posted May 23, 2007 Yes it does resize, currently just once, using this part here; //path to store images $path_thumbs = "userimages/thumbs"; $path_big = "userimages/normal"; //the new width of the resized image. $img_thumb_width = 115; // in pixels $extlimit = "no"; //Do you want to limit the extensions of files uploaded (yes/no) //allowed Extensions $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp"); Quote Link to comment Share on other sites More sharing options...
trq Posted May 23, 2007 Share Posted May 23, 2007 Move this... $query="UPDATE images SET imagelocation='$path_big/$rand_name.$file_ext', thumblocation='$path_thumbs/$rand_name.$file_ext' WHERE username = '$username'"; mysql_query($query); Into the first if clause. This way it is only executed when the form is actually submitted. Quote Link to comment Share on other sites More sharing options...
textbox Posted May 23, 2007 Author Share Posted May 23, 2007 Nice one, thanks Thorpe. Any ideas about the multiple resizes?! Nick Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 How does this work? <?php //check file extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "Wrong file extension. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>"; exit(); } ?> Where are the two variables defined? <?php $extlimit $limitedext ?> Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 textbox you have already asked this question before. Image upload extension « on: May 14, 2007, 01:26:37 PM » http://www.phpfreaks.com/forums/index.php/topic,140634.0.html Quote Link to comment Share on other sites More sharing options...
textbox Posted May 23, 2007 Author Share Posted May 23, 2007 Oh yeah, sorry about that. Did try looking because I had a small niggle that i had, but couldnt find it. Delete the old thread if needs be. They are defined in the include file show above. or again here; <?php //path to store images $path_thumbs = "userimages/thumbs"; $path_big = "userimages/normal"; //the new width of the resized image. $img_thumb_width = 115; // in pixels $extlimit = "no"; //Do you want to limit the extensions of files uploaded (yes/no) //allowed Extensions $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp"); //check if folders are Writable or not //please CHOMD them 777 if (!is_writeable($path_thumbs)){ die ("Error: The directory <b>($path_thumbs)</b> is NOT writable"); } if (!is_writeable($path_big)){ die ("Error: The directory <b>($path_big)</b> is NOT writable"); } ?> Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 You're using radios or check boxes for this right? <?php //check file extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "Wrong file extension. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>"; exit(); } ?> Quote Link to comment Share on other sites More sharing options...
textbox Posted May 23, 2007 Author Share Posted May 23, 2007 No. Just and upload file (browse) field and a submit button. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 textbox are there two files being uploaded when after the script run because I am getting two? Quote Link to comment Share on other sites More sharing options...
textbox Posted May 23, 2007 Author Share Posted May 23, 2007 Yes Trium there are. And original. And a thumbnail. Do you know how to make 3 upload, and set each one to a different dimension whilst maintaining aspect ratio?! Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 Explain this please because I really donnot understand it. <?php if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "Wrong file extension. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>"; exit(); } ?> Quote Link to comment Share on other sites More sharing options...
textbox Posted May 23, 2007 Author Share Posted May 23, 2007 I dont understand myself. Thats why I have asked for help on this issue! Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 I dont understand myself. Thats why I have asked for help on this issue! Where did you get this code? Quote Link to comment Share on other sites More sharing options...
textbox Posted May 23, 2007 Author Share Posted May 23, 2007 Sorry, its one that i have had on my computer for a long time. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted May 23, 2007 Share Posted May 23, 2007 Sorry, its one that i have had on my computer for a long time. Someone has moved it out of PHP Help! 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.