Home Backend Development PHP Tutorial PHP function that counts directory file size

PHP function that counts directory file size

Nov 22, 2016 am 11:40 AM
File size

<? 
/**
  统计目录文件大小的函数
  @author xfcode
  @link http://www.jbxue.com
*/
 function dirsize($dir)
 {
   @$dh = opendir($dir);
   $size = 0;
   while ($file = @readdir($dh))
  {
    if ($file != "." and $file != "..")
   {
     $path = $dir."/".$file;
      if (is_dir($path))
     {
       $size += dirsize($path);
      }
     elseif (is_file($path))
     {
       $size += filesize($path);
      }
    }
   }
  @closedir($dh);
  return $size;
 }
//function end 
//eg:
 $dir_path = "./my_files";
 $dir_size = dirsize($dir_path);
 $dir_size = $dir_size/1024/1024;
 echo $dir_size."MB";
 ?>
Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java program to get the size of a given file in bytes, kilobytes and megabytes Java program to get the size of a given file in bytes, kilobytes and megabytes Sep 06, 2023 am 10:13 AM

The size of a file is the amount of storage space that a specific file takes up on a specific storage device, such as a hard drive. The size of a file is measured in bytes. In this section, we will discuss how to implement a java program to get the size of a given file in bytes, kilobytes and megabytes. A byte is the smallest unit of digital information. One byte equals eight bits. One kilobyte (KB) = 1,024 bytes, one megabyte (MB) = 1,024KB, one gigabyte (GB) = 1,024MB and one terabyte (TB) = 1,024GB. The size of a file usually depends on the type of file and the amount of data it contains. Taking a text document as an example, the file size may be only a few kilobytes, while a high-resolution image or video file may be

How to get file size using C++? How to get file size using C++? Jun 01, 2024 pm 02:22 PM

Question: How to get file size in C++? Answer: 1. Use the std::ifstream::tellg() member function to get the number of bytes read or written since opening the file stream; 2. Use std::filesystem::directory_iterator to traverse the files in the directory, and Use std::ifstream::tellg() to calculate the number of bytes in each file and add them up to get the total size.

Get file size using filesize() function in PHP Get file size using filesize() function in PHP Jun 27, 2023 pm 03:14 PM

PHP is a server-side scripting language widely used in web development and is designed to provide support for the creation of dynamic web pages. One of the commonly used operations is to get the file size. File size is important to web developers as they need to ensure that the content of their website is not too large to impact the user experience. In PHP, you can use the filesize() function to get the file size. The syntax of this function is as follows: filesize(string$filename):float

PHP image operation: how to get the size and file size of images PHP image operation: how to get the size and file size of images Aug 26, 2023 am 08:55 AM

PHP Image Operation: How to Get the Size and File Size of Images In developing websites or applications, we often need to process images. Obtaining the size and file size of images is a common requirement, which can be easily achieved through some functions in PHP. This article will introduce how to use PHP to obtain the size and file size of images, and attach a code example. Get the image size To get the image size, you can use PHP's built-in function getimagesize(). This function will return a file containing the image size

How to determine the file size of a compressed package through PHP ZipArchive? How to determine the file size of a compressed package through PHP ZipArchive? Jul 21, 2023 pm 08:33 PM

How to determine the file size of compressed packages through PHPZipArchive? Summary: PHP's ZipArchive class is a very useful tool that can be used to create, modify and extract compressed archives. However, sometimes we may need to judge the file size in the compressed package so that we can be prepared when dealing with large files. In this article, we will learn how to determine the size of the files in the compressed package through PHP's ZipArchive class. Steps: First, we need to create a Z

How to compare the file sizes of compressed packages through PHP ZipArchive? How to compare the file sizes of compressed packages through PHP ZipArchive? Jul 21, 2023 pm 06:07 PM

How to compare the file sizes of compressed packages through PHPZipArchive? In actual development, we may need to compare the sizes of files in the compressed package in order to process them accordingly. PHP provides the ZipArchive class, which can easily operate on compressed packages. This article will introduce how to use PHPZipArchive to compare the file sizes in compressed packages. First, we need to make sure that the PHP Zip extension is installed and enabled. This can be done via phpinfo(

C program to find file size C program to find file size Aug 25, 2023 pm 06:17 PM

This is a C program to find the size of a file. AlgorithmBegin functionfindfileSize() Openafilepointerfpinreadonlymode. Iffpisequalstonullthen Print "Filenotfound" andreturn-1.&nbs

PHP gives file information PHP gives file information Mar 21, 2024 pm 08:06 PM

This article will explain in detail the information about the files provided by PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Introduction to PHP getting file information PHP provides a series of built-in functions to get information about files, including file size, type, modification time and file permissions. These functions are essential for file management, upload validation, and data manipulation. Get file size The filesize() function returns the size of the specified file in bytes. The fstat() function returns an array containing file status information, including file size. Get the file type The filetype() function returns the type of file (such as file, directory, symbol

See all articles