Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Unlink Not working

Tags:

php

unlink

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?

like image 803
Sunil Avatar asked Sep 26 '13 11:09

Sunil


People also ask

What does the unlink () function do in PHP?

Unlink() function: The unlink() function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure.

How do I delete a file in PHP?

To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.


1 Answers

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.

like image 56
Shakti Patel Avatar answered Oct 07 '22 22:10

Shakti Patel