Two ways to create multi-level directories in php
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

