-
-
/** - * Get all files in the current directory and subdirectories
- * @param string $dir path name
- * @return array Path array of all files
- */
- function get_files1($dir) {
- $files = array();
if (!is_dir($dir)) {
- return $files;
- }
$handle = opendir($dir);
- if($handle) {
- while(false !== ( $file = readdir($handle))) {
- if ($file != '.' && $file != '..') {
- $filename = $dir . "/" . $file;
- if(is_file ($filename)) {
- $files[] = $filename;
- }else {
- $files = array_merge($files, get_files($filename));
- }
- }
- } // end while
- closedir($handle );
- }
- return $files;
- } // end function
-
Copy code
Method 2, use glob
The glob() function finds all file paths that match pattern according to the rules used by the libc glob() function, similar to the rules used by ordinary shells.
No abbreviation expansion or parameter substitution is performed.
Returns an array containing matching files/directories. Returns FALSE if an error occurs.
This function does not work on remote files; the file being checked must be accessed through the server's file system.
This function is used to search for files in a certain directory, and it can be called an artifact.
Example:
-
-
- /**
- * Get all files in the current directory
- * @param string $dir path name
- * @return array Path array of all files
- */
- function get_files($dir) {
- $dir = realpath($dir) . "/";
- $files = array();
if (!is_dir($dir)) {
- return $files;
- }
$pattern = $dir . "*" ;
- $file_arr = glob($pattern);
foreach ($file_arr as $file) {
- if (is_dir($file)) {
- $temp = get_files($file);
if (is_array($temp)) {
- $files = array_merge($files, $temp);
- }
- }else {
- $files[] = $file;
- } / / end if
- }
- return $files;
- } // end function
- ?>
-
Copy code
Method 3, use directory class
Fake object-oriented mechanisms for reading a directory.
The dir() function opens a directory handle and returns an object. This object contains three methods: read(), rewind() and close(). And there are two properties available. The handle attribute can be used in other directory functions such as readdir(), rewinddir() and closedir(). The path attribute is set to the path of the directory being opened.
If successful, the function returns a directory stream, otherwise it returns false and an error. You can hide error output by prepending "@" to the function name.
Note: The order of directory entries returned by the read method is system dependent.
Note: This function defines the internal class Directory, which means that the user's own class cannot be defined with the same name.
Example:
-
-
- /**
- * Recursively display all files in the currently specified directory
- * Use the dir function
- * @param string $dir directory address
- * @return array $files File list
- * @site bbs.it-home.org
- */
- function get_files($dir) {
- $files = array();
if (!is_dir($dir)) {
- return $files;
- }
$d = dir($dir);
- while (false !== ($file = $d- >read())) {
- if ($file != '.' && $file != '..') {
- $filename = $dir . "/" . $file;
- < ;p>if(is_file($filename)) {
- $files[] = $filename;
- }else {
- $files = array_merge($files, get_files($filename));
- }
- }
- }
- $d ->close();
- return $files;
- }
-
Copy code
Method 4, use RecursiveDirectoryIterator class
This method is valid since PHP 5.0
Example:
-
-
- /**
- * Use RecursiveDirectoryIterator to traverse files and list all file paths
- * @param RecursiveDirectoryIterator $dir specifies the RecursiveDirectoryIterator instance of the directory
- * @return array $files file list
- */
- function get_files($dir) {
- $files = array();
for (; $dir->valid(); $dir->next()) {
- if ($dir->isDir() && !$dir->isDot()) {
- if ($dir ->haschildren()) {
- $files = array_merge($files, get_files($dir->getChildren()));
- };
- }else if($dir->isFile()){
- $ files[] = $dir->getPathName();
- }
- }
- return $files;
- }
$path = "/var/www";
- $dir = new RecursiveDirectoryIterator ($path);
- print_r(get_files($dir));
-
Copy code
|