PHP Unlink Not working
Asked Answered
M

8

11

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?

Moneybag answered 26/9, 2013 at 11:19 Comment(6)
You cannot delete using an url. Did you try a full filesystem path, like /var/www/mysite.com/img1.jpg?Overturf
Is there any error? Maybe you don't have permissions to delete this file or the file is somewhere else on the server. unlink with http://... address won't work, you have to use server path.Pincenez
I have used it as : unlink('img1.jpg'); but not workingMoneybag
paste this http://www.mysite.com/img1.jpg in your browser and check image is appearing if not YOUR PATH IS WRINGSteroid
I am sure I am not making syntax error because I have used it a site before which works fine. Is there any server issue?Moneybag
I had the same issue right now. Unlinking or renaming with absolute path e.g. /home/test/someFile.txt and filePermissions of 777 did not work. I just added the line chmod($fileName, 777); before unlink/rename and it did work. I don't know why but even while my file browser told me the permissions are 777 they didn't seem to be 777.Mountaintop
T
7

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.

Thorr answered 26/9, 2013 at 11:21 Comment(1)
Yeah working with getcwd() but I dnt know why not with absolute path. Anyways Thanks Patel . Reg, SunilMoneybag
H
3

Even though unlink() supports URLs (see here) now, http:// is not supported: http wrapper information

use a filesystem path to delete the file.

Hanson answered 26/9, 2013 at 11:24 Comment(0)
S
2

If you use unlink in a linux or unix you should also check the results of is_writable ( string $filename ) And if the function returns false, you should check the file permissions with fileperms ( string $filename ).

File permissions are usual problems on webspaces, e.g. if you upload an file per ftp with a ftp user, and the webserver is running as an different user.

If this is the problem, you have do to a

chmod o+rwd img1.jpg

or

chmod 777 img1.jpg

to grand write (and delete) permissions for other Users.

Sortition answered 26/9, 2013 at 11:48 Comment(0)
T
2

unlink($fileName); failed for me.
Then I tried using the realpath($fileName) function as unlink(realpath($fileName)); it worked.

Just posting it, in case if any one finds it useful.

php unlink

Tournedos answered 26/4, 2019 at 11:40 Comment(1)
Fair Warning! realpath() only takes path e.g. "/windows/system32" and not "/windows/system32/temp.jpg". So I used something like this unlink(realpath($tempLoc)."/".$fileName);Excurvature
D
1

use filesystem path,
first define path like this:

define("WEB_ROOT",substr(dirname(__FILE__),0,strlen(dirname(__FILE__))-3));

and check file is exist or not,if exist then unlink the file.

$filename=WEB_ROOT."img1.jpg";
if(file_exists($filename))
{
$img=unlink(WEB_ROOT."img1.jpg");
}
Debutante answered 26/9, 2013 at 11:30 Comment(0)
P
0

unlink won't work with unlink('http://www.mysite.com/img1.jpg');

use instead unlink($_SERVER['DOCUMENT_ROOT'].'img1.jpg');//takes the current directory or,

unlink($_SERVER['DOCUMENT_ROOT'].'dir_name/img1.jpg');

There may be file permission issue.please check for this.

Py answered 26/9, 2013 at 11:26 Comment(0)
R
0

Give relative path from the folder where images are kept to the file where you are writing script. If file structure is like:

-your php file
-images
  -1.jpg

then 

unlink(images/1.jpg);

Or there may be some folder permission issue. Your files are on a server or you are running it on localhost? If it is on a server then give 755 permission to the images folder.

Repartee answered 26/9, 2013 at 11:27 Comment(0)
R
0

you are using url insted of path, here is how you should do it... i am assuming your are uploading the picture to public_html. i suggest you to create a folder for pictures.

unlink("public_html/img1.jpg");
Rowden answered 22/3, 2022 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.