Bladephp: Now with 7+ codeigniter, php, and laravel project 7+ php project From $18/Year From Rs. 1000/Year

How to convert bytes Into KB, MB and GB using PHP

by
Category:php
php html
How to convert bytes Into KB, MB and GB using PHP

In this simple article we will show you how to convert bytes Into KB, MB and GB using PHP. This is a simple PHP code that converts bytes Into KB, MB and GB. You may also like How to convert Seconds into Minutes and Hours using PHP and How to Currency Converter using PHP and Google API.

HTML Code

<html>
    <body>
        <form method="post"action="">
            <input type="text" name="size" placeholder="Enter Bytes">
            <select name="convert_unit">
                <option>KB</option>
                <option>MB</option>
                <option>GB</option>
            </select>
            <br>
            <input type="submit" name="convert_size" value="Convert Bytes">
        </form>
        <p><?php echo $size; ?></p>
    </body>
</html>

PHP Code

<?php
$size = '';
function convert($size,$unit) 
 {
    if ($unit == "KB") {
        return $fileSize = round($size / 1024, 4) . 'KB';
    }
    if ($unit == "MB") {
        return $fileSize = round($size / 1024 / 1024, 4) . 'MB';
    }
    if ($unit == "GB") {
        return $fileSize = round($size / 1024 / 1024 / 1024, 4) . 'GB';
    }
}

if (isset($_POST['convert_size'])) {
    $size = $_POST['size'];
    $unit = $_POST['convert_unit'];
    $size = convert($size, $unit);
}
?>

0 Comments


Html code work in comment box


Related Post