move_d('ttt/', '../');
/**
* name: move_d
* author: Rain
* createTime: 2010-10-22
* function: 文件夹移动或拷贝
* param:
$source:需要移动的文件夹名称
$target:移动到目标路径,如果包含文件夹名称则新建一个文件
$is_move:是否为移动,默认TRUE,移动后删除$source文件夹,若FALSE则为copy
* return: 函数是否执行成功的Boolean值
**/
function move_d($source, $target, $is_move = true)
{
if(!file_exists($source))
return false;
$dest_name = basename($source);
if(is_dir($source))
{
if(file_exists($target.$dest_name) || !mkdir($target.$dest_name))
{
return false;
}
$d = opendir($source);
while(($entry = readdir($d)) !== false)
{
if(is_dir($source.$entry))
{
if($entry == "." || $entry == "..")
{
continue;
}
else
{
move_d("$source$entry/","$target$dest_name/");
}
}
else
{
if(!copy("$source$entry", "$target$dest_name/$entry"))
{
return false;
}
}
}
closedir($d);
}
else
{
if(!copy("$source$entry","$target$dest_name/"))
{
return false;
}
}
if($is_move)
{
deletedirectory($source);
}
return true;
}
function deletedirectory($dir)
{
if($dh = opendir($dir))
{
while(($file = readdir($dh)) != false)
{
if(($file == ".") || ($file == ".."))
{
continue;
}
if(is_dir($dir.'/'.$file))
{
deletedirectory($dir.'/'.$file);
}
else
{
unlink($dir.'/'.$file);
}
}
closedir($dh);
rmdir($dir);
}
}
?>