在PHP中,可以使用以下幾種方式來遍歷目錄:
$dir = opendir('/path/to/directory');
while (($file = readdir($dir)) !== false) {
echo $file . "\n";
}
closedir($dir);
$files = glob('/path/to/directory/*');
foreach ($files as $file) {
echo $file . "\n";
}
$dir = new RecursiveDirectoryIterator('/path/to/directory');
$files = new RecursiveIteratorIterator($dir);
foreach ($files as $file) {
echo $file . "\n";
}
這些方法都有各自的特點和用途,可以根據實際需求選擇合適的遍歷方式。