Jump to content

need help with unlink function


doforumda

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.