Home > Article > Backend Development > PHP iterate through directories and save
递归方式:
<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教程有兴趣的朋友有所帮助。