Heim  >  Artikel  >  Backend-Entwicklung  >  PHP durchläuft Verzeichnisse und speichert

PHP durchläuft Verzeichnisse und speichert

WBOY
WBOYOriginal
2016-07-29 09:11:54918Durchsuche

递归方式:

<code><span><span>function</span><span>getfiles</span><span>(<span>$path</span>,<span>$arr</span> = array<span>()</span>)</span>{</span><span>foreach</span>(scandir(<span>$path</span>) <span>as</span><span>$afile</span>){                           
        <span>if</span>(<span>$afile</span>==<span>'.'</span>||<span>$afile</span>==<span>'..'</span>) <span>continue</span>; 
        <span>if</span>(is_dir(<span>$path</span>.<span>'/'</span>.<span>$afile</span>)) {                       
            <span>$tmp</span> = getfiles(<span>$path</span>.<span>'/'</span>.<span>$afile</span>,<span>$arr</span>);
            <span>$arr</span> += <span>$tmp</span>;
        } <span>else</span> {
            <span>$key</span> = substr(<span>$afile</span>,strlen(<span>'hotdict_json_old_'</span>),<span>6</span>);
            <span>if</span>(<span>empty</span>(<span>$arr</span>[<span>$key</span>])){
                <span>$arr</span>[<span>$key</span>][] = <span>$afile</span>;
            }<span>else</span>{  
                array_push(<span>$arr</span>,<span>$afile</span>);
            }
        }
    }       
    <span>return</span><span>$arr</span>;
} </code>

循环方式:

<code><span><span>function</span><span>scanfiles</span><span>(<span>$dir</span>)</span> {</span><span>if</span> (! is_dir ( <span>$dir</span> ))
        <span>return</span><span>array</span>();
    <span>$dir</span> = rtrim(str_replace(<span>'\\'</span>,<span>'/'</span>,<span>$dir</span>),<span>'/'</span>).<span>'/'</span>;
    <span>$dirs</span> = <span>array</span>(<span>$dir</span>);
    <span>$rt</span> = <span>array</span>();
    <span>do</span> {
        <span>$dir</span> = array_pop(<span>$dirs</span>);
        <span>$tmp</span> = scandir(<span>$dir</span>);
        <span>foreach</span> ( <span>$tmp</span><span>as</span><span>$f</span> ) {
            <span>if</span> (<span>$f</span> == <span>'.'</span> || <span>$f</span> == <span>'..'</span>)
                <span>continue</span>;
            <span>$path</span> = <span>$dir</span>.<span>$f</span>;
            <span>if</span> (is_dir(<span>$path</span>)){
                array_push(<span>$dirs</span>,<span>$path</span>.<span>'/'</span>);
                <span>$rt</span>[<span>$f</span>] = <span>array</span>();
            }<span>else</span><span>if</span>(is_file(<span>$path</span>)){ 
                <span>if</span>(!strstr(<span>$f</span>,<span>'alpha'</span>)){
                    <span>if</span>(strstr(<span>$f</span>,<span>'all'</span>)){
                        <span>$key</span> = substr(<span>$f</span>,strlen(<span>'hotdict_all_json_old_'</span>),<span>6</span>);
                    }<span>else</span>{
                        <span>$key</span> = substr(<span>$f</span>,strlen(<span>'hotdict_json_old_'</span>),<span>6</span>);
                    }
                    array_push(<span>$rt</span>[<span>$key</span>],<span>$f</span>);
                }
            }
        }
    } <span>while</span>(<span>$dirs</span>); 
    <span>return</span><span>$rt</span>;
}</code>

以上代码有些逻辑是为了解决实际问题,请忽略,看懂代码即可!

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

版权声明:转载请保留文章署名和链接

以上就介绍了PHP遍历目录并保存,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn