0

I have a very simple function:

unlink($oldPicture);

if (is_readable($oldPicture)) {
    echo 'The file is readable';
} else {
    echo 'The file is not readable';
}
}

The file shows not readable after execution and disappears from the file directory. However, it's still available when accessed from the browser despite not being cached (opened the file on separate browsers for testing). Is there something I am missing here? Is the file being cached by the server? That is the only explanation I could come up with.

  • Consider [this suggestion](https://stackoverflow.com/a/14997103/1575353) - The question might be slightly different - have you already checked the permissions on the file? – Sᴀᴍ Onᴇᴌᴀ Jun 09 '17 at 22:30
  • Possible duplicate of [unlink cannot delete file](https://stackoverflow.com/q/14997043/6521116) – LF00 Jun 09 '17 at 22:31
  • Given your comment on the answer by Rostyslav (i.e. "... _but the file is still available in any browser as if it was being cached_ ") perhaps caching limits on the files needs to be set, likely in the web-server... – Sᴀᴍ Onᴇᴌᴀ Jun 09 '17 at 22:39

1 Answers1

0

Try something like:

if (is_file($oldPicture)) {

   chmod($oldPicture, 0777);

   if (unlink($oldPicture)) {
      echo 'File deleted';
   } else {
      echo 'Can\'t remove file';
   }

  } else {
    echo 'File does not exist';
  }

Make sure you have full path for $oldPicture

Example:

$oldPicture = dirname(__FILE__) . '/oldpicture.png';
Rost
  • 185
  • 1
  • 5
  • 19
  • 1
    I did try that, and it worked the same way. I think the unlink function is working properly, as the file is no longer in the directory, but the file is still available in any browser as if it was being cached. – Jonathan Nelson Jun 09 '17 at 22:37
  • Permissions on the file have nothing to do with unlinking it, permissions on the directory control that. – Barmar Jun 09 '17 at 23:01