PHP: unlink cannot delete file even the file exists and writable
Asked Answered
D

5

7

I've been trying to figure out why unlink is not working. I've tried stackoverflow previous questions and answers but no luck. The exact filename that needs to be deleted is 'upload/test.png'. First I made a check if file exists.

$filename = 'upload/test.png';
if(file_exists($filename)){
// file_exists returns true
    if(is_writable($filename)){
        // is_writable also returns true
        if(unlink($filename)){
            echo 'file deleted';
        }
        else{
            echo 'cant delete file';
            print_r(error_get_last());
            // this gives me
            // unlink() function.unlink: No such file or directory
        }
    }
}
Defer answered 11/10, 2013 at 12:3 Comment(6)
give complete physical path of file, on windows starting from C: or on linux /...Kassie
realpath() and $_SERVER options, or full path also return error. As the checking is done, we can assume that the file is read before the unlink executes thus it means that the file has a working path.Defer
Are you working on a live server or from your computer? Try using virtual paths ./upload/test.png and make sure you have write permissions?Sharrisharron
I tested your code, it is working perfectly. Also if file is not present at the location, it shows blank screen but no errors.Obreption
@Sharrisharron is_writable returns true. i've tried virtual paths but still gives error.Defer
@vkas. the file is present as i'm looking at it when i run the script.Defer
C
6

Give the full path instead, like

$filename = dirname(__FILE__) . '/upload/test.png';

Then try this,

if (is_file($filename)) {

   chmod($filename, 0777);

   if (unlink($filename)) {
      echo 'File deleted';
   } else {
      echo 'Cannot remove that file';
   }

} else {
  echo 'File does not exist';
}
Capone answered 11/10, 2013 at 12:23 Comment(0)
U
2

If you are saying that everything is ok and no permission issue then you can try this way too:

unlink(realpath("upload/test.png"));
Underproduction answered 11/10, 2013 at 12:23 Comment(1)
Working for me perfectly!!Underproduction
A
0

Try this and post what output you get (if any).

$filename = 'upload/test.png';

@unlink($filename);

if(is_file($filename)) {
   echo "file was locked (or permissions error)";
}
Analyze answered 11/10, 2013 at 12:14 Comment(4)
is_file returns nothingDefer
@Defer It returns either false or true. Since it didn't return true, then that file was deleted.Capone
the file is still here.Defer
You're using a relative path, don't mean to sound condescending but are you sure you're not looking in a different folder? Try changing the path to an absolute path! /path/to/the/file/upload/test.png or C:/path/to/the/file/upload/test.pngAnalyze
B
0

I have found out that unlink is sensitive to encoding. I also had such kind of problem, but then I used:

$filename= iconv("UTF-8", "Windows-1251", $filename);

and that worked for me.

Britt answered 27/9, 2020 at 15:10 Comment(0)
C
0

I was trying to use my php script to modify a file in a subdirectory of where the script was running. When I tried to delete the file, it was not possible. However, when I moved it to a completely different part of the filesystem, it worked. Seems like a security feature of php (8.1).

Contribution answered 19/2 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.