php递归创建目录的方法
墨初 编程开发 864阅读
下面给大家分享一个利用php脚本来递归创建目录的方法,但只能递归创建两层目录但实用性也是很大的。
php递归创建目录的方法
方法1:
/**
* @name 递归创建目录,只支持两层新目录的创建
* @param string $path 创建的目录路径(绝对路径)
*
* @return bool
* @https://www.73so.com
*/
function mk_dir($path)
{
if(is_dir($path)){
return false;
}
if(is_dir(dirname($path))){
mkdir($path,0755,true);
}
if(!is_dir(dirname($path))){
mk_dir(dirname($path),0755,true);
mkdir($path,0755,true);
}
return true;
}
//使用方法
$path = 'wwww/wwwroot/xxx.com/style/img/';
mk_dir($path);
方法2:
方法2中的示例与方法1中的示例其创建目录的作用与原理是一样的,只是代码的写法不同而已,但个人推荐使用方法1里面的代码。
/**
* @name 递归创建目录,只支持两层新目录的创建
* @param string $path 创建的目录路径(绝对路径)
*
* @return bool
* @https://www.73so.com
*/
function mk_dir($path)
{
if(is_dir($path)){
return;
}
return is_dir(dirname($path) || mk_dir(dirname($path) ? mkdir($path,0755,true) : false;
}