squiblo Posted August 2, 2009 Share Posted August 2, 2009 This script is for uploading profile pictures and it works perfectly but when i want to change my current profile picture it doesnt overwrite my image in FTP but does overwrite it in my database, ive tried everything but nothing seems to work, please help. <?php session_start(); include("checklogin.php"); $_SESSION['myusername']; $username = $_SESSION['myusername']; if ($_POST['submit']) { //get file attributes $name = $_FILES['myfile']['name']; $tmp_name = $_FILES['myfile']['tmp_name']; if ($name) { //start upload process $location = "./profileimages/$name"; move_uploaded_file($tmp_name,$location); $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error()); die("Your profile picture have been upload! <a href='profile.php'>Profile</a>"); } else die("Please select a file!"); } echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>"; echo "Upload your image: <form action='upload.php' method='POST' enctype='multipart/form-data'> File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'> </form> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/ Share on other sites More sharing options...
watsmyname Posted August 2, 2009 Share Posted August 2, 2009 This script is for uploading profile pictures and it works perfectly but when i want to change my current profile picture it doesnt overwrite my image in FTP but does overwrite it in my database, ive tried everything but nothing seems to work, please help. <?php session_start(); include("checklogin.php"); $_SESSION['myusername']; $username = $_SESSION['myusername']; if ($_POST['submit']) { //get file attributes $name = $_FILES['myfile']['name']; $tmp_name = $_FILES['myfile']['tmp_name']; if ($name) { //start upload process $location = "./profileimages/$name"; move_uploaded_file($tmp_name,$location); $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error()); die("Your profile picture have been upload! <a href='profile.php'>Profile</a>"); } else die("Please select a file!"); } echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>"; echo "Upload your image: <form action='upload.php' method='POST' enctype='multipart/form-data'> File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'> </form> "; ?> Do one thing delete the old picture using unlink function, and upload the new one Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888982 Share on other sites More sharing options...
squiblo Posted August 2, 2009 Author Share Posted August 2, 2009 i have no idea how to do that Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888986 Share on other sites More sharing options...
watsmyname Posted August 2, 2009 Share Posted August 2, 2009 i have no idea how to do that <?php //your other codes $old_picture_path="/images/user1.jpg"; unlink($old_picture_path); //your other codes ?> Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888990 Share on other sites More sharing options...
squiblo Posted August 2, 2009 Author Share Posted August 2, 2009 do i insert that before, "//get file attributes"? Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888992 Share on other sites More sharing options...
watsmyname Posted August 2, 2009 Share Posted August 2, 2009 do i insert that before, "//get file attributes"? first check whether user has changed the image or not while updating their profile. This you can do by comparing filename from the database and the filename that comes from the $_POST var. if its same you dont need to replace, if its not, then insert the code before //get file attributes. Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888993 Share on other sites More sharing options...
squiblo Posted August 2, 2009 Author Share Posted August 2, 2009 so it would be something like, if $_POST == $location then continue else $old_picture_path="/images/user1.jpg"; unlink($old_picture_path); ?? Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888997 Share on other sites More sharing options...
watsmyname Posted August 2, 2009 Share Posted August 2, 2009 do i insert that before, "//get file attributes"? first check whether user has changed the image or not while updating their profile. This you can do by comparing filename from the database and the filename that comes from the $_POST var. if its same you dont need to replace, if its not, then insert the code before //get file attributes. or to check whether user has change their profile image or not, if file field is empty then probably they havent changed their picture so you dont need to update picture field and delete it. if its not empty you need to delete as well as update database Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-888998 Share on other sites More sharing options...
squiblo Posted August 2, 2009 Author Share Posted August 2, 2009 i cant get my head around this one, im quite new to php Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889002 Share on other sites More sharing options...
watsmyname Posted August 2, 2009 Share Posted August 2, 2009 i cant get my head around this one, im quite new to php if($_FILES['myfile']['name']!=""){ //this means user has browsed for new picture to upload $old_picture_path="/images/user1.jpg"; //change your path unlink($old_picture_path); //then do upload of new picture and update database with new picture location } //get file attributes // your codes Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889006 Share on other sites More sharing options...
squiblo Posted August 2, 2009 Author Share Posted August 2, 2009 error <?php session_start(); include("checklogin.php"); $_SESSION['myusername']; $username = $_SESSION['myusername']; if($_FILES['myfile']['name']!=""){ //this means user has browsed for new picture to upload $old_picture_path="/images/user1.jpg"; //change your path unlink($old_picture_path); //then do upload of new picture and update database with new picture location } //get file attributes $name = $_FILES['myfile']['name']; $tmp_name = $_FILES['myfile']['tmp_name']; if ($name) { //start upload process $location = "./profileimages/$name"; move_uploaded_file($tmp_name,$location); $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error()); die("Your profile picture have been upload! <a href='profile.php'>Profile</a>"); } else die("Please select a file!"); } echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>"; echo "Upload your image: <form action='upload.php' method='POST' enctype='multipart/form-data'> File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'> </form> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889009 Share on other sites More sharing options...
watsmyname Posted August 2, 2009 Share Posted August 2, 2009 error what error you get?? anyway here is your code again, i have modified it <?php session_start(); include("checklogin.php"); $_SESSION['myusername']; $username = $_SESSION['myusername']; if ($_POST['submit']) { //get file attributes $name = $_FILES['myfile']['name']; $tmp_name = $_FILES['myfile']['tmp_name']; if ($name) { //start upload process /*in database just save image name dont save whole location of the image, your path remains same always, just the imagename changes and wherever you are showing image then fetch image name from database and use the location which you already know. write a query to get image name from the table for the user $username. say you have imagename in variable $oldimage*/ $old_picture_path="./profileimages/".$oldimage; unlink($old_picture_path); //new image $location = "./profileimages/$name"; move_uploaded_file($tmp_name,$location); $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error()); die("Your profile picture have been upload! <a href='profile.php'>Profile</a>"); } else die("Please select a file!"); } echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>"; echo "Upload your image: <form action='upload.php' method='POST' enctype='multipart/form-data'> File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'> </form> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889019 Share on other sites More sharing options...
squiblo Posted August 2, 2009 Author Share Posted August 2, 2009 this is how i am viewing the image but the old image still didnt get deleted from FTP <?php session_start(); include("checklogin.php"); $username = $_SESSION['myusername']; $query = mysql_query("SELECT * FROM members WHERE username='$username'"); if (mysql_num_rows($query)==0){ die("User not found!"); }else{ $row = mysql_fetch_assoc($query); $location = $row['imagelocation']; if($location == ""){ echo "<img src ='/profileimages/box.png' width='225' height='275' border='0'></a>"; }else{ echo "<img src ='$location' width='225' height='275' border='0'>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889031 Share on other sites More sharing options...
squiblo Posted August 2, 2009 Author Share Posted August 2, 2009 the latest script doesnt work (does not overwrite the image in FTP) and i do not know how to change it Quote Link to comment https://forums.phpfreaks.com/topic/168522-delete-picture/#findComment-889052 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.