Rumah > php教程 > php手册 > teks badan

php创建多级目录的两种方法

WBOY
Lepaskan: 2016-10-28 15:03:28
asal
1152 orang telah melayarinya

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>
Salin selepas log masuk

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>
Salin selepas log masuk
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan