Say we have a loop like this:
foreach($entries as $entry){
if (file_exists('/some/dir/'.$entry.'.jpg')){
echo 'file exists';
}
}
I assume that it should access the hard drive 1000 times and check if every file exists.
How about this one?
$files = scandir('/some/dir/');
foreach($entries as $entry){
if (in_array($entry.'.jpg', $files)){
echo 'file exists';
}
}
Question 1: If it accesses the hard drive once, I believe that it should be much faster. Am I right on that?
However, if I need to check subdirectories for a file, for example:
foreach($entries as $entry){
if (file_exists('/some/dir/'.$entry['id'].'/'.$entry['name'].'.jpg')){
echo 'file exists';
}
}
Question 2: If I want to apply the technique described above (files in an array) to check if there are entries, how can I scandir()subdirectories in an array so that I can compare the existence of a file using this method?
source
share