首頁 >php教程 >php手册 >php建立多層目錄的兩種方法

php建立多層目錄的兩種方法

WBOY
WBOY原創
2016-10-28 15:03:281180瀏覽

1.使用遞歸的思想

<span style="color: #0000ff">function</span> mkdirs_2(<span style="color: #800080">$path</span><span style="color: #000000">){
</span><span style="color: #0000ff">if</span>(!<span style="color: #008080">is_dir</span>(<span style="color: #800080">$path</span><span style="color: #000000">)){
mkdirs_2(</span><span style="color: #008080">dirname</span>(<span style="color: #800080">$path</span><span style="color: #000000">));
</span><span style="color: #0000ff">if</span>(!<span style="color: #008080">mkdir</span>(<span style="color: #800080">$path</span>, 0777<span style="color: #000000">)){
</span><span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
}
}
</span><span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
}
</span><span style="color: #008000">/*</span><span style="color: #008000"> http://www.manongjc.com/article/1331.html </span><span style="color: #008000">*/</span>
<span style="color: #800080">$path2</span> = 'sdfs/sds/sds/s/s/sss'<span style="color: #000000">;
</span><span style="color: #008080">var_dump</span>(mkdirs_2(<span style="color: #800080">$path2</span>)); <span style="color: #008000">//</span><span style="color: #008000">true;</span>

1.思想主要是利用了遞歸, 先創建dir, 在創建dir/css ,在創建…. 

2.遞歸就是入棧的過程, 所以先讓,dir/css/js/php/ok 先入棧, 則它會最後出棧…其它的就不說了.
3.dirname(path)返回的是path中除了最後一個目錄的值, 即第一次回傳是: dir/css/js/php,,,第二次回傳dir/css/js,,,最後一次是./


2.直接利用mkdir()建立, 第三個參數必須是true才能自動建立多層目錄

<span style="color: #0000ff">function</span> mkdirs_1(<span style="color: #800080">$path</span>, <span style="color: #800080">$mode</span> = 0777<span style="color: #000000">){
</span><span style="color: #0000ff">if</span>(<span style="color: #008080">is_dir</span>(<span style="color: #800080">$path</span><span style="color: #000000">)){
</span><span style="color: #0000ff">return</span> '无法创建,已经是目录了'<span style="color: #000000">;
}</span><span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #0000ff">if</span>(<span style="color: #008080">mkdir</span>(<span style="color: #800080">$path</span>, <span style="color: #800080">$mode</span>, <span style="color: #0000ff">true</span><span style="color: #000000">)) {
</span><span style="color: #0000ff">return</span> '创建成功'<span style="color: #000000">;
}</span><span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #0000ff">return</span> '创建失败'<span style="color: #000000">;
}
}
}
</span><span style="color: #008000">/*</span><span style="color: #008000"> http://www.manongjc.com/article/1332.html </span><span style="color: #008000">*/</span>
<span style="color: #800080">$path1</span> = 'a/b/c/d/e'<span style="color: #000000">;
</span><span style="color: #008080">var_dump</span>(mkdirs_1(<span style="color: #800080">$path1</span>)); <span style="color: #008000">//</span><span style="color: #008000">string '创建成功' (length=12)</span>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:捷運下一篇:捷運