PHP statistics directory of files and directories in the directory size method, _php tutorial

Source: Internet
Author: User
Tags glob

PHP statistics directory of files and directories in the directory size method,


The examples in this article describe the files in the PHP statistics directory and the size of directories in the directory. Share to everyone for your reference, as follows:

<?php//loops through all the files in the directory and counts the size of directories and files $dirName = "PhpMyAdmin"; $dir =opendir ($dirName); Returns a resource type while ($fileName =readdir ($dir)) {$file = $dirName. " /". $fileName; if ($fileName! = "." && $fileName! = "...") {if (Is_dir ($file)) {echo "". $fileName. " = = = ". Date (" Y-m-d h:i:s ", Filectime ($file))." = = ". FileType ($file)." = = ". Tosize (Dirsize ($file))."
"; } else{echo "". $fileName. " ===== ". Date (" Y-m-d h:i:s ", Filectime ($file))." = = = ". FileType ($file)." = = = ". Tosize (FileSize ($file))."
"; }}} closedir ($dir); Convert the size of a file or directory into an easy-to-read way function tosize ($size) {$DW;//Specify the unit of file or directory statistics if ($size >pow (2,30)) {$DW = "GB"; $size =round ($size/pow (2,30), 2); } else if ($size >pow (2,20)) {$DW = "MB"; $size =round ($size/pow (2,20), 2); } else if ($size >pow (2,10)) {$DW = "KB"; $size =round ($size/pow (2,10), 2); } else {$dw = "bytes";} return $size. $dw; }//Use recursion to count the size of the directory function dirsize ($dirName) {$dirsize =0; $dir =opendir ($dirName); while ($fileName =readdir ($dir)) {$ file= $dirName. " /". $fileName; if ($fileName! = "." && $fileName! = "...") {//must be judged, otherwise an error will occur if (Is_dir ($file)) {$dirsize +=dirsize ($file); } else{$dirsize +=filesize ($file); }}} closedir ($dir); return $dirsize; }?>

Read more about PHP files and directory operations related content readers can view this site topic: "PHP file Operation Summary" and "PHP Directory Operation skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP Implementation Statistics Directory file size function
    • The file directory operation of PHP surface question
    • PHP uses recursive methods to list all files in the current directory
    • How PHP recursively iterates through the files of a specified directory and counts the number of files
    • Php method for separating file directories and filenames from the full file path
    • How PHP deletes all directories and files in the specified directory
    • PHP recursive method of deleting directories and files
    • PHP recursive method for reading directories and files
    • How PHP uses the Glob function to traverse a directory or folder
    • PHP uses the Glob function to quickly query the method of specifying a directory file
    • The basic operation of PHP file directory

http://www.bkjia.com/PHPjc/1089928.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089928.html techarticle PHP Statistics Directory of files and directories in the directory size method, this article describes the PHP statistics directory files and directories in the directory size method. Share to everyone for reference, with ...

  • 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.