Using PHP, I want to get the content from the first file in a folder and when the content is loaded, delete the file. Here is what I have:
$a = opendir('./');
while (false !== ($entry = readdir($a))) {
if($entry != '.' && $entry != '..') {
echo $entry = file_get_contents('./'.$entry.'', FILE_USE_INCLUDE_PATH);
break; // only need the first file
}
}
The code above loads the first file in the folder and I can delete it successfully using something like
unlink("temp.txt");
So there are no permission denied errors. BUT what I need to do is delete the file by its variable name (because every filename is different). Surprisingly for me, unlink("$entry");
or something similar does not let me delete it, instead showing a warning along with the first few lines of the content of that file. If I echo $entry
It shows temp.txt correctly. Can someone enlighten me? What am I missing here?
(Optional (un)related question: If I have numeric files like 1.txt, 2.txt, 3.txt, 10.txt... . Is there a way I could modify the code above in a way, that it does not load files like 1,10,2,3 ..., instead load it like 1,2,3,10...?)
UPDATE: The updated code that works (for future reference):
$a = opendir('./');
while (false !== ($entry = readdir($a))) {
if($entry != '.' && $entry != '..') {
echo $b = file_get_contents('./'.$entry.'', FILE_USE_INCLUDE_PATH);
break; // only need the first file
}
}
unlink("./$entry");