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.
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.
The functionality of this script can be broken down as follows:
-
Directory Traversal: The script starts at a specified root directory. It then recursively explores each subdirectory, going as deep as the directory structure allows.
-
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.
-
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.
-
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. -
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