Reputation: 3067
I am uploading files with file_put_contents. Is there any way i can calculate file size like we do with *move_uploaded_file* ? I believe string length and file_size are two dufferent things.
Upvotes: 1
Views: 3257
Reputation: 1167
There is a function called filesize()
that calculates the size of a file. You pass the filepath as a parameter:
$filesize = filesize("myfiles/file.txt");
You could then use a function like this to format the filesize to make it more user friendly:
function format_bytes($a_bytes) {
if ($a_bytes < 1024) {
return $a_bytes .' B';
} elseif ($a_bytes < 1048576) {
return round($a_bytes / 1024, 2) .' KB';
} elseif ($a_bytes < 1073741824) {
return round($a_bytes / 1048576, 2) . ' MB';
} elseif ($a_bytes < 1099511627776) {
return round($a_bytes / 1073741824, 2) . ' GB';
} elseif ($a_bytes < 1125899906842624) {
return round($a_bytes / 1099511627776, 2) .' TB';
} elseif ($a_bytes < 1152921504606846976) {
return round($a_bytes / 1125899906842624, 2) .' PB';
} elseif ($a_bytes < 1180591620717411303424) {
return round($a_bytes / 1152921504606846976, 2) .' EB';
} elseif ($a_bytes < 1208925819614629174706176) {
return round($a_bytes / 1180591620717411303424, 2) .' ZB';
} else {
return round($a_bytes / 1208925819614629174706176, 2) .' YB';
}
}
Upvotes: 1
Reputation: 19979
According to the docs relating to file_put_contents return value:
The function returns the number of bytes that were written to the file, or FALSE on failure.
So you should be able to do something like:
$filesize = file_put_contents($myFile, $someData);
Upvotes: 5