Home >Backend Development >PHP Tutorial >How to read all files in a specified directory in php?
<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.