PHP methods for directory operations

墨辰丷
Release: 2023-03-31 10:20:01
Original
1496 people have browsed it

This article mainly introduces PHP methods for directory operations. Interested friends can refer to it. I hope it will be helpful to everyone.

The example in this article describes how PHP calculates the size of the entire directory. The specific implementation method is as follows:

/**
 * Calculate the full size of a directory
 *
 * @author   Jonas John
 * @version   0.2
 * @param    string  $DirectoryPath  Directory path
 */
function CalcDirectorySize($DirectoryPath) {
  // I reccomend using a normalize_path function here
  // to make sure $DirectoryPath contains an ending slash
  // To display a good looking size you can use a readable_filesize
  // function.
  $Size = 0;
  $Dir = opendir($DirectoryPath);
  if (!$Dir)
    return -1;
  while (($File = readdir($Dir)) !== false) {
    // Skip file pointers
    if ($File[0] == '.') continue; 
    // Go recursive down, or add the file size
    if (is_dir($DirectoryPath . $File))      
      $Size += CalcDirectorySize($DirectoryPath . $File . DIRECTORY_SEPARATOR);
    else 
      $Size += filesize($DirectoryPath . $File);    
  }
  closedir($Dir);
  return $Size;
}
//使用范例:
$SizeInBytes = CalcDirectorySize('data/');
Copy after login

Summary: The above is the entire content of this article. I hope it can help Everyone’s learning helps.

Related recommendations:

php unordered tree implementation skills

php process control and mathematical operations

PHP sends post, get requests and operates cookies based on curl

The above is the detailed content of PHP methods for directory operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template