How to get the size of a file in MB (Megabytes)?
Use the length()
method of the File
class to return the size of the file in bytes.
// Get file from file name
File file = new File("U:\intranet_root\intranet\R1112B2.zip");
// Get length of file in bytes
long fileSizeInBytes = file.length();
// Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
long fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
long fileSizeInMB = fileSizeInKB / 1024;
if (fileSizeInMB > 27) {
...
}
You could combine the conversion into one step, but I've tried to fully illustrate the process.
Format bytes to kilobytes, megabytes, gigabytes
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
// $bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
(Taken from php.net, there are many other examples there, but I like this one best :-)
How do I implode a json object to a string in php?
$json_object = '[{"125":"1"},{"126":"2"},{"127":"3"},{"128":"4"},{"129":"5"},{"130":"6"},{"131":"7"},{"132":"8"},{"133":"9"},{"134":"10"},{"135":"11"}]';
$json = json_decode($json_object);
echo implode(", ", array_map(function($obj) { foreach ($obj as $p => $v) { return $p;} }, $json));
echo "<br>";
echo implode(", ", array_map(function($obj) { foreach ($obj as $p => $v) { return $v;} }, $json));
See https://3v4l.org/p3p45
Related Topics
Calculate Total Sum of Dynamically Added Items to a Table
How to Find If a Text Contains Url String
How to Use Nestjs Logging Service
How to Compress an Image Via JavaScript in the Browser
Javascript Generate Random Numbers Without Repeating
Angular: Toggle Active Class on Only Button the Current Clicked Button (Not Using *Ngfor)
How to Prevent the Keyboard from Popping Up on Mobile Devices
Filter Json Object to String Array by Key Value
Reactjs, Table Cell With Different Background Color
Electron, After Browserify, Fs.Existssync Is Not a Function
Sort Json Data Based on Date and Time
Regular Expression Which Allow Only Characters or Space
Converting Text to Unicode in JavaScript
Node.Js MySQL Query in a for Loop
Javascript Submit Multiple Forms With One Button
How to Clear Location.State in React-Router on Page Reload
Sticky Header Smoothing Scroll Down
How to Split Date and Time from the Datetime Variable in JavaScript