/**
- * Get all files in current directory and sub-directory
- * @param string $dir path name
- * @return Array of paths for all files in array
- */
- 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 CodeMethod 2, use the Globglob () function to find all file paths that match the pattern in accordance with the rules used by the libc glob () function, similar to the rules used for general shells. No abbreviation extensions or parameter overrides are performed. Returns an array that contains a matching file/directory. Returns FALSE if an error occurs. This function does not work on remote files, and the files being inspected must be accessed through the server's file system. This function is used to search for files in a directory, which is an artifact. Example:
/**
- * Get all the files in the current directory
- * @param string $dir path name
- * @return Array of paths for all files in array
- */
- 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 CodeMethod 3, use the directory-based phishing object-oriented mechanism to read a directory. The Dir () function opens a directory handle and returns an object. This object consists of three methods: Read (), rewind (), and Close (). And there are two properties available. The handle property can be used in other directory functions such as Readdir (), Rewinddir (), and Closedir (). The Path property is set to the directory path that is opened. If successful, the function returns a directory stream, otherwise it returns false and an error. You can hide the output of the error by adding "@" before the function name. Note: The order of the catalog entries returned by the Read method depends on the system. Note: This function defines the internal class Directory, which means that the user's own class can no longer be defined with the same name. Example:
/**
- * Recursively displays all files in the current 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;
if (Is_file ($filename)) {
- $files [] = $filename;
- }else {
- $files = Array_merge ($files, Get_files ($filename));
- }
- }
- }
- $d->close ();
- return $files;
- }
Copy CodeMethod 4, using the Recursivedirectoryiterator class This method is valid from 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 |