php文件是可写的但无法删除

I am using this function. is_file and is_writable return true, but when I true to unlink, it gives an error. This is on windows server.

if(is_file($fileToDelete)) {
  if(is_writable($fileToDelete)) {
    unlink($fileToDelete);
  }
}

The file is a PDF document, which I have open. I thought is_writable would return false in this case, but it doesn't.

So how can I tell if a file can be deleted or not?

Thank you

What about doing it the other way around? Just try to delete the file and check whether it is really gone?

@unlink($fileToDelete);

if(is_file($fileToDelete)) {
   // file was locked (or permissions error)
}

Not sure whether this is workable in your specific case though, but judging by the code in your question this should be what you want.

Are you using the file? I mean, did you open it by doing fopen($file)?

Do a fclose($file) before trying to delete the file.

For them who don't want to delete the file before the check, the solution is here :

$file = "test.pdf";

if (!is_file($file)) {
    print "File doesn't exist.";
} else {
    $fh = @fopen($file, "r+");
    if ($fh) {
        print "File is not opened and seems able to be deleted.";
        fclose($fh);
    } else {
        print "File seems to be opened somewhere and can't be deleted.";
    }
}

Simple, and efficient.