Home  >  Article  >  php教程  >  PHP删除整个文件夹和移动整个文件夹

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

WBOY
WBOYOriginal
2016-06-21 09:06:301032browse

 // ========== 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 ==========




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
Previous article:PHP生成动态WAP页面Next article:高级PHP V5 对象研究