PHP Statistics Directory size custom function sharing _php tips

Source: Internet
Author: User

Calculating the size of files, disk partitions, and directories is a common task in a variety of applications. The size of the computed file can be accomplished by the filesize () function described earlier, and the statistics disk size can also be implemented using Disk_free_space () and Disk_total_space () two functions. But PHP does not currently provide a standard function for the total size of the catalog, so we're going to customize a function to accomplish this task. First, consider whether the computed directory contains any other subdirectories, and if there are no subdirectories, the total size of all files under the directory is the size of the directory. If a subdirectory is included, then the size of the directory is computed in this way, and recursive functions appear to be the best fit for this task. The custom functions that calculate the size of the directory are as follows:

Copy Code code as follows:

<?php
Customize a function Dirsize () to count the directory size of incoming parameters
function Dirsize ($directory) {
$dir _size = 0; Used to accumulate individual file sizes

if ($dir _handle = @opendir ($directory)) {//Open directory and determine if it will open successfully
while ($filename = Readdir ($dir _handle)) {//Loop through all the files in the directory
if ($filename!= "." && $filename!= "...") {//Be sure to exclude two special directories
$subFile = $directory. "     /". $filename; Connect the files in the directory to the current directory
if (Is_dir ($subFile))//If the directory
$dir _size + + dirsize ($subFile); Recursively call its own function to find the size of the subdirectory
if (Is_file ($subFile))//If the file
$dir _size + + filesize ($subFile); Find the size of the file and add it up
}
}
Closedir ($dir _handle); To close a file resource
return $dir _size; Returns the calculated directory size
}
}

$dir _size = dirsize ("phpMyAdmin"); Call this function to calculate the directory size
Echo Round ($dir _size/pow (1024,1), 2). "    KB "; Converts the number of bytes to a "KB" unit and outputs
?>

You can also use the Exec () or system () function to call the operating system command "Du" to return the size of the directory. For security reasons, however, these functions are typically disabled and are not conducive to cross-platform operations.

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.