Home >Backend Development >PHP Tutorial >PHP gets all files in the specified directory (including subdirectories)

PHP gets all files in the specified directory (including subdirectories)

WBOY
WBOYOriginal
2016-07-29 09:00:202292browse

PHP Gets all files (including subdirectories) in the specified directory

The test environment is under Linux. If you want to use Windows, please correct the directory path search method in $new_dir

<?<span>php
 
function get_file_list($dir)
{
  </span><span>    $file_list </span>=<span> array();
    $file_dir_list </span>=<span> array();
     
    $dir_list </span>= scandir($dir); <span>//</span><span>查找目录  </span><span>foreach</span> ($dir_list <span>as</span><span> $r)
    {
        </span><span>if</span> ($r == <span>'</span><span>.</span><span>'</span> || $r == <span>'</span><span>..</span><span>'</span><span>)  
        {
            </span><span>continue</span><span>;
        }
        $new_dir </span>= $dir . <span>'</span><span>/</span><span>'</span><span> . $r;
        </span><span>if</span><span> (is_dir($new_dir))
        {</span><span>            $file_dir </span>=<span> get_file_list($new_dir);
            $file_dir_list </span>=<span> array_merge($file_dir_list, $file_dir);
        }
        </span><span>else</span><span>        {
            $file_list[] </span>=<span> $new_dir;
        }
    }
     
    </span><span>return</span><span> array_merge($file_list, $file_dir_list);
}
 
$file_list </span>= get_file_list(<span>'</span><span>.</span><span>'</span><span>);
print_r($file_list);</span>

scandir definition and usage

scandir() The function returns an array containing the files and directories in the specified path.

If successful, return an array, if failed, return false. If directory is not a directory, returns boolean false and generates an E_WARNING level error.

Syntax

scandir(directory,sort,context)
ParametersDescription
directoryRequired. Specifies the directories to be scanned.
sortoptional. Specify the order of sorting. Default is 0 (ascending). If 1, descending order.
contextOptional. Specifies the environment for directory handles. context is a set of options that modify the behavior of the directory stream.

Example

<?<span>php
print_r(scandir(</span><span>"</span><span>images</span><span>"</span><span>));
</span>?> 

Output:

<span>Array
(
[</span><span>0</span>] =><span> .
[</span><span>1</span>] =><span> ..
[</span><span>2</span>] =><span> dog.jpg
[</span><span>3</span>] =><span> house.jpg
[</span><span>4</span>] =><span> logo.gif
)</span>

The above introduces PHP to obtain all files (including subdirectories) in the specified directory, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn