Home >Backend Development >PHP Tutorial >How to read all files in a specified directory in php?

How to read all files in a specified directory in php?

WBOY
WBOYOriginal
2016-08-08 09:22:451254browse

<code><span><span>function</span><span>treeDirectory</span><span>(<span>$dir</span>)</span>{</span><span>$files</span> = <span>array</span>();
    <span>$dirpath</span> = realpath(<span>$dir</span>);
    <span>$filenames</span> = scandir(<span>$dir</span>);

    <span>foreach</span> (<span>$filenames</span><span>as</span><span>$filename</span>) 
    {
        <span>if</span> (<span>$filename</span>==<span>'.'</span> || <span>$filename</span>==<span>'..'</span>)
        {
            <span>continue</span>;
        }

        <span>$file</span> = <span>$dirpath</span> . DIRECTORY_SEPARATOR . <span>$filename</span>;

        <span>if</span> (is_dir(<span>$file</span>))
        {
            <span>$files</span> = array_merge(<span>$files</span>, <span>self</span>::treeDirectory(<span>$file</span>));
        }
        <span>else</span>
        {
            <span>$files</span>[] = <span>$file</span>;
        }
    }

    <span>return</span><span>$files</span>;
}</code>

The above introduces how to read all files in the specified directory in php? , including relevant 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