分享一个php代码创建目录的Demo

时间:2023-03-09 09:00:29
分享一个php代码创建目录的Demo
/*
* 连续建目录
* string $dir 目录字符串
* int $mode 权限数字
* 返回:顺利创建或者全部已建返回true,其它方式返回false
*/
function makeDir( $dir_path, $mode = "0777" ) {
//如:路径("c:/testweb/wap/home.php"), 我们要创建的目录》c:/testweb/wap,所以在上面$dir_path='c:testweb/wap'即可
if( ! $dir_path ) return 0;
$dir_path = str_replace( "\\", "/", $dir_path );
$mdir = "";
foreach( explode( "/", $dir_path ) as $val ) {
$mdir .= $val."/";
if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
if( ! file_exists( $mdir ) ) {
if(!@mkdir( $mdir, $mode )){
return false;
}
}
}
return true;
}