PHP reads directory tree

WBOY
Release: 2016-07-29 08:51:19
Original
2076 people have browsed it

There was such a question in the written test of the interview with XX company a while ago:

Use PHP to list the directory tree!

I was stunned when I saw it! The basic idea is still there, but it uses a recursive algorithm, but I don’t know much about the method of operating directory files in PHP, so I’ll take a good refresher course today!
Problem-solving ideas:

  1. List the files in the directory.
  2. Determine whether it is a directory. If it is a directory, continue recursing.
  3. Save all file names into a multi-dimensional array
<code><span><?php </span><span><span>function</span><span>recurDir</span><span>(<span>$pathName</span>)</span>
{</span><span>//将结果保存在result变量中</span><span>$result</span> = <span>array</span>();
    <span>$temp</span> = <span>array</span>();
    <span>//判断传入的变量是否是目录</span><span>if</span>(!is_dir(<span>$pathName</span>) || !is_readable(<span>$pathName</span>)) {
        <span>return</span><span>null</span>;
    }
    <span>//取出目录中的文件和子目录名,使用scandir函数</span><span>$allFiles</span> = scandir(<span>$pathName</span>);
    <span>//遍历他们</span><span>foreach</span>(<span>$allFiles</span><span>as</span><span>$fileName</span>) {
        <span>//判断是否是.和..因为这两个东西神马也不是。。。</span><span>if</span>(in_array(<span>$fileName</span>, <span>array</span>(<span>'.'</span>, <span>'..'</span>))) {
            <span>continue</span>;
        }
        <span>//路径加文件名</span><span>$fullName</span> = <span>$pathName</span>.<span>'/'</span>.<span>$fileName</span>;
        <span>//如果是目录的话就继续遍历这个目录</span><span>if</span>(is_dir(<span>$fullName</span>)) {
            <span>//将这个目录中的文件信息存入到数组中</span><span>$result</span>[<span>$fullName</span>] = recurDir(<span>$fullName</span>);
        }<span>else</span> {
            <span>//如果是文件就先存入临时变量</span><span>$temp</span>[] = <span>$fullName</span>;
        }
    }
    <span>//取出文件</span><span>if</span>(<span>$temp</span>) {
        <span>foreach</span>(<span>$temp</span><span>as</span><span>$f</span>) {
            <span>$result</span>[] = <span>$f</span>;
        }
    }
    <span>return</span><span>$result</span>;
}
<span>//验证一下这个函数是否好用!</span><span>$tree</span> = recurDir(<span>'cart_ajax'</span>);
<span>echo</span><span>"<pre class="brush:php;toolbar:false">"
Copy after login
; print_r($tree); echo""; ?>

cart_ajax folder structure

PHP reads directory tree

Contents under the js folder

PHP reads directory tree

Display results

PHP reads directory tree

Done ! The level of the directory is indeed like this, but it seems too low! Spruce it up a bit!

<code><span><span>function</span><span>beautifulTree</span><span>(<span>$arr</span>, <span>$l</span> = <span>'-|'</span>)</span>
{</span><span>static</span><span>$l</span> = <span>''</span>;
    <span>static</span><span>$str</span> = <span>''</span>;
    <span>//遍历刚才得到的目录树</span><span>foreach</span>(<span>$arr</span><span>as</span><span>$key</span>=><span>$val</span>) {
        <span>//如果是个数组,也就代表它是个目录,那么就在它的子文件中加入-|来表示是下一级吧</span><span>if</span>(is_array(<span>$arr</span>[<span>$key</span>])) {
            <span>$str</span>.=<span>$l</span>.<span>$key</span>.<span>"<br>"</span>;
            <span>$l</span>.=<span>'-|'</span>;
            beautifulTree(<span>$arr</span>[<span>$key</span>], <span>$l</span>);
        }<span>else</span> {
            <span>$str</span>.=<span>$l</span>.<span>$val</span>.<span>"<br>"</span>;
        }
    }
    <span>$l</span> = <span>''</span>;
    <span>return</span><span>$str</span>;
}

<span>$beautifulTree</span> = beautifulTree(<span>$tree</span>);
<span>echo</span><span>"<pre class="brush:php;toolbar:false">"
Copy after login
; print_r($beautiful); echo"";

Show results

PHP reads directory tree

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces PHP to read the directory tree, including aspects of the 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