Home  >  Article  >  Backend Development  >  How to create a directory recursively in php, recursive creation in php_PHP tutorial

How to create a directory recursively in php, recursive creation in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:251289browse

How to create a directory recursively in php, create recursively in php

The example in this article describes the method of recursively creating a directory in PHP, and I would like to share it with you for your reference.

The specific implementation code is as follows:

<&#63;php
function mk_dir($path){
 //第1种情况,该目录已经存在
 if(is_dir($path)){
 return;
 }
 //第2种情况,父目录存在,本身不存在
 if(is_dir(dirname($path))){
 mkdir($path);
 }
 //第3种情况,父目录不存在
 if(!is_dir(dirname($path))){
 mk_dir(dirname($path));//创建父目录
 mkdir($path);
 }
}
$path = './e/b/c/f';
mk_dir($path);
&#63;>

Replaced with ternary operation, the code is as follows:

<&#63;php
function mk_dir($path){
 //第1种情况,该目录已经存在
 if(is_dir($path)){
 return;
 }
 //三元运算
 return is_dir(dirname($path)||mk_dir(dirname($path)&#63;mkdir($path):false;
}
$path = './e/b/c/f';
mk_dir($path);
&#63;>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/951640.htmlTechArticleHow to create a directory recursively in php, php recursive creation This article describes the method of recursively creating a directory in php, share it with everyone For your reference. The specific implementation code is as follows: phpfunction mk_...
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