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

WBOY
Release: 2016-08-08 09:22:45
Original
1239 people have browsed it

<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>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template