次のようなループがあるとします。
foreach($entries as $entry){ // let's say this loops 1000 times
if (file_exists('/some/dir/'.$entry.'.jpg')){
echo 'file exists';
}
}
これは、HDDに1000回アクセスして、各ファイルが存在するかどうかを確認する必要があると想定しています。
代わりにこれを行うのはどうですか?
$files = scandir('/some/dir/');
foreach($entries as $entry){ // let's say this loops 1000 times
if (in_array($entry.'.jpg', $files)){
echo 'file exists';
}
}
質問 1:これが HDD に 1 回アクセスする場合、はるかに高速になるはずです。私はこれで正しいですか?
ただし、次のように、ファイルのサブディレクトリを確認する必要がある場合はどうなりますか。
foreach($entries as $entry){ // let's say this loops 1000 times
if (file_exists('/some/dir/'.$entry['id'].'/'.$entry['name'].'.jpg')){
echo 'file exists';
}
}
質問 2:上記の手法 (配列内のファイル) を適用してエントリが存在するかどうかを確認する場合、scandir()
この方法を使用してファイルの存在を比較できるように、配列にサブディレクトリを追加するにはどうすればよいですか?