|
|
|
| Title : |
File size with label - afunction that reads the filesize from a selected file and convert the number of bytes into KB, MB etc |
| Categories : |
PHP, Filesystem |
 Olaf Lederer |
| Date : |
Dec 08th 2004 |
| Grade : |
3 of 5 (graded 2 times) |
| Viewed : |
3785 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Olaf Lederer |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
This function reads the filesize from a selected file and convert the number of bytes into KB, MB etc. Just define the file and the path and the size is give by the function.
| <?php
// the $file without the path, put the path in the $path-variable (based on the doc. root)
function file_size($file, $path = "") {
global $DOCUMENT_ROOT;
$bytes = array("B", "KB", "MB", "GB", "TB", "PB");
$file_with_path = $DOCUMENT_ROOT."/".$path."/".$file;
// replace (possible) double slashes with a single one
$file_with_path = str_replace("//", "/", $file_with_path);
$size = filesize($file_with_path);
$i = 0;
while ($size >= 1024) { //divide the filesize (in bytes) with 1024 to get "bigger" bytes
$size = $size/1024;
$i++;
}
if ($i > 1) {
// you can change this number if you like (for more precision)
return round($size,1)." ".$bytes[$i];
} else {
return round($size,0)." ".$bytes[$i];
}
}
?> | |
Usage Example
[code]
<?php
// example for using
echo file_size("example.txt", "myFolder");
?> |
|
| Unix Disk Information with graphs Categories : PHP, Shell Scripting, Filesystem | | | directory, opendir, listfiles, files in a directory, get directory Categories : PHP, Filesystem | | | Show Source with Line Numbers Categories : PHP, Regexps, Filesystem | | | How to find the name of the current file? Categories : PHP, Filesystem, Strings | | | Opening and formatting text files into HTML on the fly- or HTML from templates. Categories : PHP, HTML and PHP, Filesystem | | | GuestBook Light - a plug and play application for any website. Categories : PHP, Complete Programs, Filesystem, Sessions | | | Relative Path Categories : PHP, Filesystem | | | Image Browser Categories : Filesystem, GD image library, Content Management, PHP | | | Differences between two files Categories : PHP, Filesystem, Tip | | | Random Image Display Categories : PHP, Filesystem, Graphics, HTML and PHP | | | Handle multiple file upload Categories : Complete Programs, Filesystem, PHP, HTML and PHP | | | Directory viewer, customize how you display the file structure, easy to
understand. Found out about PHP 3 days ago, and this is my first prog. Categories : HTML and PHP, Complete Programs, Directories, Filesystem, PHP | | | basename -- Returns filename component of path Categories : PHP, PHP Functions, Filesystem | | | Link Submition - Allow your visitors to submit links to the site. Categories : PHP, Arrays, Filesystem, Beginner Guides | | | A simple configuration file editor to ease you life in setting up php applications. Reads variables from a given file automatically and displays current value. New value will be written to file after submit. Categories : PHP, Filesystem, Regexps, Java Script | |
| |
| |
|