首页 >php教程 >php手册 >PHP删除整个文件夹和移动整个文件夹

PHP删除整个文件夹和移动整个文件夹

WBOY
WBOY原创
2016-06-21 09:06:301073浏览

 // ========== doDelDir函数 START ==========
        function doDelDir($dir)
        {
            $dh=opendir($dir);
            while ($file=readdir($dh))
            {
                if($file!="." && $file!="..")
                {
                    $fullpath=$dir."/".$file;
                    if(!is_dir($fullpath))
                    {
                        unlink($fullpath);
                    }
                    else
                    {
                        $this->doDelDir($fullpath);
                    }
                }
            }
            closedir($dh);
            if (rmdir($dir))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
  // ========== doDelDir函数 END ==========

  // ========== doMoveDir函数 START ==========
  function doMoveDir($source,$target)
  {
   if(is_dir($source))
   {
    $dest_name=basename($source);
    if(!mkdir($target.$dest_name))
    {
     return false;
    }
    $d=dir($source);
    while(($entry=$d->read())!==false)
    {
     if(is_dir($source.$entry))
     {
      if($entry=="."||$entry=="..")
      {
       continue;
      }
      else
      {
       $this->doMoveDir("$source$entry\\","$target$dest_name\\");
      }
     }
     else
     {
      if(!copy("$source$entry","$target$dest_name\\$entry"))
      {
       return false;
      }
     }                 
    }         
   }
   else
   {
    if(!copy("$source$entry","$target$dest_name\\"))
    {
     return false;
    }         
   }         
   return true; 
  }
  // ========== doMoveDir函数 END ==========




声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn