Unlink not working?

I have written a small function to remove an image record from the DB, and the “physical” image itself from the server…


function removePhoto($tableName, $recordID, $imgPath)
{	
	$connection = connectDB(SERVER, USERNAME, PASSWORD, DATABASE);
	$deleteQuery =	"DELETE FROM ".$tableName." 
					 WHERE pk = '".$recordID."'";
	$result = mysql_query($deleteQuery, $connection);
	if($result)
	{
		if(file_exists($imgPath))
		{
			fclose($imgPath);
			$result = unlink($imgPath);
			echo "result is " . $result;
		}	
	}
	return $result;
}

It gets as far as the unlink function, and even returns “1”, however the file (image files in this case) remains on the server.

I have written other functions to remove entire directorys (recursive) and they work just fine. Any ideas where I may be going wrong in this instance?

be careful reusing the $result variable - you might be tripping yourself up.
Are you sure it’s entering the if statements? (Might wanna combine those, btw)

Ok it looks like file_exists returns false now you come to mention it. Which is really strange because I can browse directly to it via:


the variable $imgPath holds:

uploads/personal_images/foobar/15/DSC01045.JPG for example which i can access in the url address bar just fine. The plot thickens!

Is the file executing from the root of myurl.com ?

Im sorry I dont quite understand what you mean :slight_smile:

Is the file http://www.myurl.com/file.php ? or is it in a subdirectory?

its in a subdirectory;

uploads/personal_images/foobar/15

The strange thing is, if i pass the variable holding the string:

uploads/personal_images/foobar/15/DSC01045.JPG

file_exists returns false, but If i paste the string in directly e.g

if(file_exists(“uploads/personal_images/foobar/15/DSC01045.JPG”))
{
//code
}

that actually returns true!!

I meant the file you’re calling this function from.

Echo out your $imgPath beofre you get to the IF’s. Make sure it’s being passed into the function correctly.

the file im calling the function from is in http://www.myurl.com/file.php yes.

oh well, I guess lll have to play around with it!