PHP Function Filesize
File size is an important metric when it comes to managing files in a system. This is especially true when working with large files like MP3s, Zip Files and PDFs. PHP’s built in function filesize makes it easy to get the size of a file in a human readable format. The filesize() function uses the system’s underlying stat command to get the file size.
The php function filesize() takes one parameter which is the path to the file that you want to check the size of. The function returns the file size in bytes on success and FALSE on failure.
If you are primarily dealing with images and other small files then it is useful to convert the file size in bytes into KB (kilobytes). This can be done using the following simple code.
You can also use the linux command du to do this task as well. The command is fairly straightforward and should work on most systems.
In addition to being able to convert the size in bytes, it is also possible to convert it to MB (megabytes). This can be done using the cd command and the strlen function.
The results of the php function filesize() are cached, so it is important to clear this cache regularly. This can be done with the clearstatcache() function.