Home  >  Article  >  php教程  >  Two ways to create multi-level directories in php

Two ways to create multi-level directories in php

WBOY
WBOYOriginal
2016-10-28 15:03:281155browse

1. Use the idea of ​​recursion

<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. The main idea is to use recursion, first create dir, then create dir/css, then create...

2. Recursion is the process of pushing onto the stack, So let dir/css/js/php/ok be pushed onto the stack first, then it will pop out last... I won’t say anything else.
3.dirname(path) returns is the value of the path except the last directory, that is, the first return is: dir/css/js/php,, the second return is dir/css/js,, and the last return is ./


2. Create directly using mkdir(), the third parameter must be true to automatically create multi-level directories

<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>
Statement:
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
Previous article:subwayNext article:subway