WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Getting the size of a file in megs
Categories : PHP, Filesystem Click here to Update Your Picture
Justin French
Date : May 04th 2003
Grade : 2 of 5 (graded 1 times)
Viewed : 3991
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Justin French
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

PHP's filesize() function returns the size of the file in bytes, which isn't very human-readable for anything over a few k.

So, if you find you are constantly performing calculations on filesizes to:

a) convert them to megs
b) round the value

... then this should help:

<?php
function getFileSize($file,$round="1")
    {
   
$bytes = 0;
    if(
file_exists($file))
        {
       
$bytes = filesize($file);
        }
   
   
$bytes = round($bytes/1000000,$round);
    return
$bytes;
    }
?>


Usage Example:
So, for a 2572876 byte MP3 file....
<?php
$file
= "dir/dir/someFile.mp3";
$size = getFileSize($file,5);     // 2.573
$size = getFileSize($file);        // 2.6
?>


I've only included calculations for "meg" in the above example, but you can easily add to it to
calculate kb (1000 bytes), and even GB!



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
PHP based Contact email form with multiple recipients, text file based, supports departments.
Categories : PHP, Email, Beginner Guides, 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
Image Browser
Categories : Filesystem, GD image library, Content Management, PHP
Differences between two files
Categories : PHP, Filesystem, Tip
Fetching product details from the commission junction website using php
Categories : PHP, FTP, Filesystem, Compression
Single-file PHP news system with automatic folder structure creation
Categories : PHP, Filesystem, Arrays
Random Image Display
Categories : PHP, Filesystem, Graphics, HTML and PHP
A PHP Script that shows how to use FTP to run a shell script, read two local files and update data in a database.
Categories : PHP, Filesystem, FTP, Date Time, Databases
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Read DPI value from image with PHP
Categories : PHP, Graphics, Filesystem
Handle multiple file upload
Categories : Complete Programs, Filesystem, PHP, HTML and PHP
 Guy Sagnes wrote : 960
Hello,
You have to do a correction in you division:
1 kB = 1024 bytes, 1 MB = 1024*1024 bytes and so on...
 
 Justin French wrote :961
Thanks!!