A easy way to check the file size

Converts a string like "5 MB", or "400 KB" to the equivalent in Bytes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function StringSizeToBytes($Size){
 
    $Unit = strtolower($Size);
    $Unit = preg_replace('/[^a-z]/'''$Unit);
 
    $Value = intval(preg_replace('/[^0-9]/'''$Size));
 
    $Units = array('b'=>0'kb'=>1'mb'=>2'gb'=>3'tb'=>4);
    $Exponent = isset($Units[$Unit]) ? $Units[$Unit] : 0;
 
    return ($Value * pow(1024$Exponent));            
}
// Example usage:
// Check if a file is bigger than 10 MB
 
if (filesize('example.zip') > StringSizeToBytes('10 MB')){
    print 'File is to big !';
}
else {
    print 'File is okay';
}
X

Url: http://www.jonasjohn.de/snippets/php/string-size-to-bytes.htm

Language: PHP | User: ShareMySnippets | Created: Oct 16, 2013