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
        }
    }
}
nickanor
  • 637
  • 2
  • 12
  • 18
  • give complete physical path of file, on windows starting from C: or on linux /... – Sumit Gupta Oct 11 '13 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. – nickanor Oct 11 '13 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? – GroovyCarrot Oct 11 '13 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. – Vikas Arora Oct 11 '13 at 12:32
  • @GroovyCarrot is_writable returns true. i've tried virtual paths but still gives error. – nickanor Oct 11 '13 at 12:43
  • @vkas. the file is present as i'm looking at it when i run the script. – nickanor Oct 11 '13 at 12:44

4 Answers4

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';
}
Yang
  • 8,580
  • 8
  • 33
  • 58
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"));
Black
  • 18,150
  • 39
  • 158
  • 271
Satyam Saxena
  • 581
  • 2
  • 10
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)";
}
BT643
  • 3,495
  • 5
  • 34
  • 55
  • is_file returns nothing – nickanor Oct 11 '13 at 12:30
  • @nickanor It returns either `false` or `true`. Since it didn't return `true`, then that file was deleted. – Yang Oct 11 '13 at 12:32
  • the file is still here. – nickanor Oct 11 '13 at 12:37
  • 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` – BT643 Oct 11 '13 at 13:02
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.

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
AnKov
  • 1
  • 1