PHP递归创建目录函数
自己写个半死,也写不出个完美的,今天翻PHP手册,发现了这个好东西,稍改了一下,创建目录爽YY!
创建类似"../../../xxx/xxx.txt"的目录都很好!
function mkdirs($path, $mode = 0777) //creates directory tree recursively
{
$dirs = explode('/',$path);
$pos = strrpos($path, ".");
if ($pos === false) { // note: three equal signs
// not found, means path ends in a dir not file
$subamount=0;
}
else {
$subamount=1;
}
for ($c=0;$c < count($dirs) - $subamount; $c++) {
$thispath="";
for ($cc=0; $cc <= $c; $cc++) {
$thispath.=$dirs[$cc].'/';
}
if (!file_exists($thispath)) {
//print "$thispath<br>";
mkdir($thispath,$mode);
}
}
}
原函数中使用$GLOBALS["dirseparator"]我改成了'/'
function recur_mkdirs($path, $mode = 0777) //creates directory tree recursively
{
//$GLOBALS["dirseparator"]
$dirs = explode($GLOBALS["dirseparator"],$path);
$pos = strrpos($path, ".");
if ($pos === false) { // note: three equal signs
// not found, means path ends in a dir not file
$subamount=0;
}
else {
$subamount=1;
}
for ($c=0;$c < count($dirs) - $subamount; $c++) {
$thispath="";
for ($cc=0; $cc <= $c; $cc++) {
$thispath.=$dirs[$cc].$GLOBALS["dirseparator"];
}
if (!file_exists($thispath)) {
//print "$thispath<br>";
mkdir($thispath,$mode);
}
}}
我操,能更新BLOG不能上Q?我以为你病还没好住医院了~!
我操,搜索implode居然搜索到你博客来了。。。。汗一个,最近马甲公开了,不知道换什么名字了。多联系啊。
我操.............................................
这个方法也不错。自己写没仔细测过。
function mkdirs($path, $mode = 0777) //creates directory tree recursively
{
$dir = dirname($path);
if($dir != '.')
{
mkdirs($dir,$mode);
}
echo $path;
mkdir($path,$mode);
}
贴错了
function mkdirs($path, $mode = 0777) //creates directory tree recursively
{
$dir = dirname($path);
if($dir != '.')
{
mkdirs($dir,$mode);
}
if(!file_exists($path))
{
mkdir($path,$mode);
}
}
其实手册评论里,有一个写得很完美的
function mkdirs($pathname, $mode = 0755) {
is_dir(dirname($pathname)) || mkdirs(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode);
}
function mkdirs($path){
$upPath = dirname($path);
if(!is_dir($upPath)){
mkdirs($upPath);
}
mkdir($path);
return true;
}