This is my first tutorial, so cut me some slack if it's not up to the usual standards please :P
Hopefully you can learn a useful technique in PHP and learn to apply it to many different situations.
The following tutorial will explain how to get the size of a given file in bytes, and then use some basic parsing to turn the bytes into different formats (kilobytes, megabytes etc.)
Open you prefered PHP editor, and write out the following code (write don't copy, you will not learn by copying)
| <? function file_size($file){ global $size_out; $prelim_size=filesize($file); $b=$prelim_size; $kb=round($b/1024,4); $mb=round($kb/1024,4); $gb=round($mb/1024,4); $tb=round($gb/1024,4); $size_out="$file : $b Bytes$kb Kilobytes$mb Megabytes$gb Gigabytes $tb Terabytes"; return $size_out;} ?> |
That is the basic function, now to explain it.
First we open the script then declare the function and the operators. We then make $size_out global,meaning it can be used across multiple scripts and will always have the same value
We now call the size of the file in bytes using the filesize() function. We then do some simple math to figure out the value of the different size formats. We finally output $size_out;
That is the script done. Save it as file_size.php. To call the function use
<? echo file_size("path/to/file_size.php"); ?>
There are many uses of this simple script. For example, if you make a file upoad program you can see the size
of already uploaded files to see if yuo have space in the directory for the file you need to upload.
I hope you've had some fun leaerning this, and hope you can apply it to a great script.