Directory processing function
The basic idea of ??processing folders is as follows:
1. When reading a certain path, determine whether it is a folder
2. If it is a folder, open the specified folder and return Resource variables of the file directory
3. Use readdir to read the files in the directory once, and the directory pointer is shifted backward once
4. Use readdir to read to the end, and there is no readable file Return false
5. Close the file directory
Let’s learn a list of common functions:
##
<?php //設(shè)置打開的目錄是D盤 $dir = "d:/"; //判斷是否是文件夾,是文件夾 if (is_dir($dir)) { if ($dh = opendir($dir)) { //讀取一次向后移動一次文件夾指針 echo readdir($dh).'<br />'; echo readdir($dh).'<br />'; echo readdir($dh).'<br />'; echo readdir($dh).'<br />'; //讀取到最后返回false //關(guān)閉文件夾資源 closedir($dh); } } ?> 即然是讀取一次向后移動一次,我們是不是可以 <?php //設(shè)置打開的目錄是D盤 $dir = "d:/"; //判斷是否是文件夾,是文件夾 if (is_dir($dir)) { if ($dh = opendir($dir)) { //讀取到最后返回false,停止循環(huán) while (($file = readdir($dh)) !== false) { echo "文件名為: $file : 文件的類型是: " . filetype($dir . $file) . "<br />"; } closedir($dh); } } ?>