Catalog Custom Functions

Source: Internet
Author: User
PHP does not provide the function of querying directory size, copying directories, deleting directories with files, we need to write our own

This function simply passes a URL and can recursively query the file size in the directory to count the size of the directory

1 
 Php2     functionDirsize ($dir){3         $dirsize= 0;4         $url=Opendir($dir);5          while($filename=Readdir($url)){6             $file=$dir.' /'.$filename;7             if(! ($filename=='.' ||$filename=='..')){8                 if(Is_dir($file)){9                     $dirsize+ = Dirsize ($file);Ten}Else{ One                     $dirsize+=filesize($file); A                 } -             } -         } the         Closedir($url); -         return $dirsize; -}

This function needs to pass two URLs, namely the old directory and the new directory, to copy the directory and the file

1 
 Php2     functionCopydir ($DIRSRC,$dirTo){3         if(Is_file($dirTo))4         {5             Echo' Destination is not a directory cannot be created! ';6             return;7         }8         if(!file_exists($dirTo))9         {Ten             mkdir($dirTo); One         } A         $dir _handle= @Opendir($DIRSRC); -         if($dir _handle) -         { the              while($filename=Readdir($dir _handle)) -             { -                 if($filename! = "." &&$filename!="..") -                 { +                     $subSrcFile=$DIRSRC. "\\".$filename; -                     $subToFile=$dirTo. "\\".$filename; +                       A                      if(Is_dir($subSrcFile)) at                      { -Copydir ($subSrcFile,$subToFile); -                      } -                      if(Is_file($subSrcFile)) -                      { -                           Copy($subSrcFile,$subToFile); in                      } -                 } to             } +             Closedir($dir _handle); -         } the}

This function simply passes a URL, can delete the directory and subdirectories of the file recursively, thus deleting the directory

1 
 Php2 functionRmdirall ($url){3     //detects whether the directory to open exists4     if(!$dir=@Opendir($url)){5         return false;6     }7      while(false!==($file=Readdir($dir))){8     //exclude current directory from parent directory9         if($file!=="." &&$file!==".."){Ten             $file=$url.' /'.$file; One             //recursively delete a directory A             if(Is_dir($file)){ -Rmdirall ($file); -}Else{ the                 if(@unlink($file)){ -                     Continue; -}Else{ -                     return false; +                 } -             } +         } A         if(@rmdir($url)){ at            return true; -}Else{ -            return false; -         } -     } -}

The above describes the directory of custom functions, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.