Skip to content

PHP Script to get directory size and file size. List all directory and files

License

Notifications You must be signed in to change notification settings

bienthuy/linux-directory-size

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Linux Directory Size

PHP Script Get Directory Size

This script will traverse through a specified directory and its subdirectories, listing all files and folders in a tree-like structure. For each file, it will also display the file size. It will also display the total size of a directory. 

How to find size of directory and files

This is a code to check directory size, file size, and list all information in a tree-like list. You just need to upload it to a host that supports PHP language from 7.x onwards and it will run and display information when you request. It will search all directories and files at the same level with it and subdirectories, files in subdirectories…

For example, if you use shared hosting and want to check which directory or file is the heaviest, you can upload it inside the public_html directory, suppose it is located at /home/bienthuywebsite/public_html/check_folder_size.php and your domain is https://bienthuy.com then you just need to access the browser and enter the address https://bienthuy.com/check_folder_size.php The result will appear as shown in the image below.

 

php script get directory size

List files in a directory with size

The functionality of this script can be broken down as follows:

  1. Directory Traversal: The script starts at a specified root directory. It then recursively explores each subdirectory, going as deep as the directory structure allows.

  2. List files in a directory with size: For each directory it visits, the script lists all files contained within. It does this by reading the contents of the directory and filtering out any entries that are files.

  3. Show directory and file size: For each file and directory it finds, the script calculates the size and showing it. This is typically done by reading the file’s metadata, which contains information about the file size.

  4. List all directory and files in Tree-like Display: The script keeps track of the current depth in the directory structure (i.e., how many directories deep it is from the root). It uses this information to prefix each file or directory name with a series of indents or other symbols (like |- or ├──), creating a visual representation of the directory tree.

  5. Output: The script outputs the directory tree, with each file or directory on a new line. Each line includes the file or directory name, along with the file size for files.

This script is a handy tool for getting a quick overview of the structure and contents of a directory. It’s particularly useful for large directories where manually checking each file would be time-consuming. It can also be used as a basis for more complex scripts that need to perform operations on each file in a directory.


Find us on Facebook: https://www.facebook.com/bienthuywebsite

Find us on Twitter: https://twitter.com/bienthuywebsite

Find us on Google Plus: http://plus.google.com/+bienthuywebsite

Find us on Pinterest: https://www.pinterest.com/bienthuywebsite

Find us on Youtube: https://www.youtube.com/bienthuywebsite

About

PHP Script to get directory size and file size. List all directory and files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages