Maison > php教程 > php手册 > le corps du texte

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

WBOY
Libérer: 2016-10-28 15:03:28
original
1152 Les gens l'ont consulté

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>
Copier après la connexion

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>
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal