PHP Unlink Not working

Sunil picture Sunil · Sep 26, 2013 · Viewed 32.4k times · Source

I am trying to delete photo in php using unlink. I have used it earlier on other server but this time it is not working. I have used absolute path for a test but still does not works:

I have used it as: unlink('img1.jpg');

and :

unlink('http://www.mysite.com/img1.jpg');

Please anyone having such experience?

Answer

Shakti Patel picture Shakti Patel · Sep 26, 2013

url not allow in ulink function

can you please used this

It's better, also safety wise to use an absolute path. But you can get this path dynamically.

E.g. using:

getcwd();

Depending on where your PHP script is, your variable could look like this:

$deleteImage =  getcwd() . 'img1.jpg';

unlink($deleteImage);

check this

bool unlink ( string $filename [, resource $context ] )

and

filename
Path to the file.

So it only takes a string as filename.

Make sure the file is reachable with the path from the location you execute the script. This is not a problem with absolute paths, but you might have one with relative paths.