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
        }
    }
}
6
  • give complete physical path of file, on windows starting from C: or on linux /... Commented Oct 11, 2013 at 12:13
  • 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. Commented Oct 11, 2013 at 12:17
  • 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? Commented Oct 11, 2013 at 12:25
  • 1
    I tested your code, it is working perfectly. Also if file is not present at the location, it shows blank screen but no errors. Commented Oct 11, 2013 at 12:32
  • @GroovyCarrot is_writable returns true. i've tried virtual paths but still gives error. Commented Oct 11, 2013 at 12:43

5 Answers 5

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';
}
Sign up to request clarification or add additional context in comments.

Comments

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"));

1 Comment

Working for me perfectly!!
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)";
}

4 Comments

is_file returns nothing
@nickanor It returns either false or true. Since it didn't return true, then that file was deleted.
the file is still here.
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.png
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.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.