The PHP Function Disk_Total_Space
PHP has a function called disk_total_space that returns the total size of a directory in bytes. This is a handy function for server administrators and web developers who need to keep track of their disk space usage. This article takes a look at the syntax and parameters of this inbuilt function.
In this example, we use a string to represent the path and name of the file whose size is needed. This string can be any text, even the empty ''. Then, we add the '' to separate the string into a list. This list is then used to get the size of the file (in bytes). Finally, we return this information to our caller in a form that is easy to understand.
Note that this function does not work well cross-platform, and it may give nefarious users access to sensitive server disk or partition information. It also does not check that the file being examined is accessible, which could pose a security risk.
Getting the size of a file in kilobytes, megabytes, and gigabytes is very simple, since we're dealing with prefixes that are binary; the first power of 2 (1024) represents a kilobyte, and the next power of 2 (1 GB). Simply multiply the number of bytes of the file by the appropriate prefix to determine its value.
PHP provides a variety of functions for parsing a file path into its components: basename() for the base portion of the name; dirname() for the directory component of the name; and pathinfo() for all three. This is a good opportunity to brush up on your skills as a full stack developer and make sure you know these key functions.