Unlink / file_exists and file not found

I have this code in my application, which is often run in race mode by individual users of my application.

clearstatcache(TRUE, $filepath);
if(file_exists($filepath)) unlink($filepath);

But for this line of code, I still have separate errors every day, like

unlink (file): No such file or directory

The server starts Apache 2.2 and PHP 5.3.3. I know the race issue, but I think the @ operator is just evil. I first tried without any parameters for clearstatcache () with the same error. How can I do it right?

+5
source share
2 answers

As the comment says, my need is to make sure that I deleted the file so that I don’t know that the witch process deletes it, therefore

@unlink($filepath);
clearstatcache(TRUE, $filepath);
if(file_exists($filepath)) throw new Exception('file not deleted : ' . $filepath);

.

, .

+5

if(@unlink($path)) {
  echo "Deleted file "; 
}
else{
  echo "File can't be deleted";
}

, ,

+6

All Articles