permission denied - php unlink
Asked Answered
F

6

34

I have two files: b.php and test.txt

<?php 
$b = "test.txt";
unlink($b);
?>

and the error is: Warning: unlink(test.txt) [function.unlink]: Permission denied

why? b.php and test.txt is 777 and the same group/login

if I set 777 on the parent directory I can execute unlink but i have to set 777 and back to 755?

Floorage answered 27/11, 2012 at 23:10 Comment(9)
Are they in the same directory?Mame
could be that you need write permission to the directory they reside in for the process that runs the php code.Spaceband
Is this part of a website? If so, you need to make sure the webserver's process has permissions to the directory and/or file.Azalea
Check the permissions of the parent directory. Try setting 777 on the parent directory and see if that changes anythingKandis
if I set 777 on the parent directory then I can execute unlink but i have to set 777 and back to 755?Floorage
You could set it back to 755, but then you won't be able to create new files or delete existing ones until you set the mode back to 777.Fascinating
I need set 777 at folder to execute unlink?Floorage
.htaccess of captcha: deny from all, when changed to allow from all, no error message anymoreInrush
may be you should try using absolute path. if the issue persists even after permissions, <?php echo $_SERVER['DOCUMENT_ROOT']; ?> will give you absolute pathThird
F
39

You (as in the process that runs b.php, either you through CLI or a webserver) need write access to the directory in which the files are located. You are updating the directory content, so access to the file is not enough.

Note that if you use the PHP chmod() function to set the mode of a file or folder to 777 you should use 0777 to make sure the number is correctly interpreted as an octal number.

Fascinating answered 27/11, 2012 at 23:26 Comment(1)
chmod php example: chmod("/somedir/somefile", 777); php.net/manual/en/function.chmod.php Also note: The folder itself has to be executable by everyone.Bellringer
I
20

You'll first require to close the file using fclose($handle); it's not deleting because the file is in use. So first close the file and then try.

Imposition answered 24/4, 2014 at 4:24 Comment(0)
C
10

in addition to all the answers that other friends have , if somebody who is looking this post is looking for a way to delete a "Folder" not a "file" , should take care that Folders must delete by php rmdir() function and if u want to delete a "Folder" by unlink() , u will encounter with a wrong Warning message that says "permission denied"

however u can make folders & files by mkdir() but the way u delete folders (rmdir()) is different from the way you delete files(unlink())

eventually as a fact:

in many programming languages, any permission related error may not directly means an actual permission issue

for example, if you want to readSync a file that doesn't exist with node fs module you will encounter a wrong EPERM error

Contention answered 7/3, 2018 at 11:4 Comment(0)
F
2
// Path relative to where the php file is or absolute server path
chdir($FilePath); // Comment this out if you are on the same folder
chown($FileName,465); //Insert an Invalid UserId to set to Nobody Owner; for instance 465
$do = unlink($FileName);

if($do=="1"){ 
    echo "The file was deleted successfully."; 
} else { echo "There was an error trying to delete the file."; } 

Try this. Hope it helps.

Flannery answered 27/11, 2012 at 23:33 Comment(2)
chown() [function.chown]: Operation not permittedFloorage
This is probably because you don't have the right to change the ownership of the file. Can you try to change the permissions to the file to see if it changes? (e.g. chmod("test.txt", 0666). you can possibly try and unlink it on 666 to see what happens. Is there anyway the see the user who owns the file?Flannery
F
1

The file permission is okay (0777) but i think your on the shared server, so to delete your file correctly use; 1. create a correct path to your file

// delete from folder
$filename = 'test.txt';
$ifile = '/newy/made/link/uploads/'. $filename; // this is the actual path to the file you want to delete.
unlink($_SERVER['DOCUMENT_ROOT'] .$ifile); // use server document root
// your file will be removed from the folder

That small code will do the magic and remove any selected file you want from any folder provided the actual file path is collect.

Ftlb answered 2/6, 2017 at 15:2 Comment(0)
N
0

In Windows and before PHP version 7.3.0, check that your file has been closed before unlinking it,
as said in https://www.php.net/manual/en/function.unlink.php :

On Windows, it is now possible to unlink() files with handles in use, while formerly that would fail. However, it is still not possible to re-create the unlinked file, until all handles to it have been closed.

As an exemple :

$fullFilePath = 'C:\Users\MyUserName\www\myApp\public\test.txt';
$handle = fopen($fullFilePath , 'w+');

fopen($filePath, 'w+');
fputs($handle, 'Some text in the file');
fclose($handle);

unlink(realpath($insertedLinesFilePath));
Narcoma answered 12/8, 2022 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.