doforumda Posted March 9, 2010 Share Posted March 9, 2010 hi i want to delete image from the server using unlink function but it is not working. it does not delete the image and it does not display any errors as well. here is my code <?php session_start(); include("dbConnect.php"); $photo = @$_POST['photo']; $deletephoto = @$_POST['deletephoto']; if($photo) { $get = mysql_query("SELECT * FROM photos WHERE userid='$_SESSION[userid]' AND profile='1'"); $row = mysql_fetch_assoc($get); $profile = $row['location']; //echo $profile."<br>"; $make_profile = mysql_query("UPDATE photos SET profile='1' WHERE userid='$_SESSION[userid]' AND location='$photo'"); if($make_profile) { $unmake_profile = mysql_query("UPDATE photos SET profile='0' WHERE userid='$_SESSION[userid]' AND location='$profile'"); } } if($deletephoto) { $count = count($deletephoto); for($i = 0; $i < $count; $i++) { //echo $deletephoto[$i]."<br>"; $delete_photos = mysql_query("DELETE FROM photos WHERE userid='$_SESSION[userid]' AND location='$deletephoto[$i]'"); //echo $deletephoto[$i]; unlink($deletephoto[$i]); } } //die(); header("Location: photos.php"); ?> in second if statment i am using unlink as you can see. there $deletephoto[$i] variable has something like this "photos/image.jpg". how can this be solved to work? Quote Link to comment Share on other sites More sharing options...
scarhand Posted March 9, 2010 Share Posted March 9, 2010 need to include the file path of the file before the file variable use $_SERVER['SCRIPT_FILENAME']; to get this file path. Quote Link to comment Share on other sites More sharing options...
doforumda Posted March 9, 2010 Author Share Posted March 9, 2010 in unlink($deletephoto[$i]); $deletephoto[$i] has the path as well as image. photos is the directory and image.jpg is the image in this variable. and where should use $_SERVER['SCRIPT_FILENAME']; in my code above. Quote Link to comment Share on other sites More sharing options...
scarhand Posted March 9, 2010 Share Posted March 9, 2010 create a php file whose contents is "<?php echo $_SERVER['SCRIPT_FILENAME']; ?>" (without the "") place that php file into your photos folder open that php file in your internet browser check to make sure that path matches the one your photos are using, if not, correct the problem. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 9, 2010 Share Posted March 9, 2010 scarhand has you on the right track for this. Your first mistake is that you don't check or save the result of the unlink function. It returns true or false. The second issue is that clearly you do not have the full path of the photo file in the database. That needs to be corrected unless the path relative to this script (the photos directory) is off the current directory. Without knowing the *real* filesystem path to where the photos reside, it is hard to say what the path should be, or if scarhand's advice is helpfull, although even if it is, you probably need to use dirname($_SERVER['SCRIPT_FILENAME']) or some variation of that. The first step is to figure out where on the server the photos have actually been stored. 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.