Home  >  Article  >  Backend Development  >  php循环检测目录是否存在并创建(循环创建目录)_php技巧

php循环检测目录是否存在并创建(循环创建目录)_php技巧

WBOY
WBOYOriginal
2016-05-17 09:21:48935browse

循环创建目录方法
这个会生成image.gif目录

复制代码 代码如下:

$filepath = "test/upload/2010/image.gif";
mk_dir($filepath);
// 循环创建目录
function mk_dir($dir, $mode = 0755)
{
if (is_dir($dir) || @mkdir($dir,$mode)) return true;
if (!mk_dir(dirname($dir),$mode)) return false;
return @mkdir($dir,$mode);
}

第二种方法:
复制代码 代码如下:

$filepath = "test/upload/2010/image.gif";
createDir(dirname($filepath));
//接下来就可以move_uploaded_file了!

/*
* 功能:循环检测并创建文件夹
* 参数:$path 文件夹路径
* 返回:
*/
function createDir($path){
if (!file_exists($path)){
createDir(dirname($path));
mkdir($path, 0777);
}
}
?>
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