Home >Backend Development >PHP Tutorial >How to create a directory recursively in php, recursive creation in php_PHP tutorial
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:
<?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); ?>
Replaced with ternary operation, the code is as follows:
<?php function mk_dir($path){ //第1种情况,该目录已经存在 if(is_dir($path)){ return; } //三元运算 return is_dir(dirname($path)||mk_dir(dirname($path)?mkdir($path):false; } $path = './e/b/c/f'; mk_dir($path); ?>
I hope this article will be helpful to everyone’s PHP programming design.