The first problem is that you're overwriting $entry
with the file contents, as such the filename is no longer valid when trying to delete it (explaining the error with the file contents).
Secondly, because you're using FILE_USE_INCLUDE_PATH
you don't know exactly where the file is located, and unlink
resolves related to the current working directory, which is most probably not $a
.
Use unlink($a.'/'.$entry)
and you'll be fine.
As for the unrelated question - use scandir
to get all the files in the folder, then apply natsort
to the resulting array to sort by a 'natural sorting algorithm'. Keep in mind that a directory listing always also lists the folders .
and ..
which you'll have to detect and skip or remove manually.