1, access to directory files, not including subdirectories
// get all the files, directory names in a directory (not including subdirectories under the file, directory name) $ Handler = opendir ( $ dir ); the while (( $ filename = readdir ( $ Handler !)) == false ) { / / Always use == prevent similar filename "0" and the like occurs directory! IF ( $ filename = &&! "." $ filename = ".."! {) $ files [] = $ filename ; } } } closedir ( $ Handler ); // print all file names foreach ( $ filens as $value) { echo $value."<br />"; }
2, under the directory get all the files, including subdirectories
function get_allfiles($path,&$files) { if(is_dir($path)){ $dp = dir($path); while ($file = $dp ->read()){ if($file !="." && $file !=".."){ get_allfiles($path."/".$file, $files); } } $dp ->close(); } if(is_file($path)){ $files[] = $path; } } function get_filenamesbydir($dir){ $files = array(); get_allfiles($dir,$files); return $files; } $filenames = get_filenamesbydir("static/image/"); //打印所有文件名,包括路径 foreach ($filenames as $value) { echo $value."<br />"; }
}