Jump to content

how to get the size of a file from directory.


Sajesh Mohan

Recommended Posts

                                        $file="../myclients/clients/".$company_name."/Download/".$file_name;

$size = filesize($path); // not working

 

i try with  function  also

 

    function formatbytes($file, $type) 

    { 

        switch($type){ 

            case "KB": 

                $filesize = filesize($file) * .0009765625; // bytes to KB 

            break; 

            case "MB": 

                $filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB 

            break; 

            case "GB": 

                $filesize = ((filesize($file) * .0009765625) * .0009765625) * .0009765625; // bytes to GB 

            break; 

        } 

        if($filesize <= 0){ 

            return $filesize = 'unknown file size';} 

        else{return round($filesize, 2).' '.$type;} 

    } 

 

echo formatbytes($file, "KB");

 

 

 

uploaded file size is 488 kb 

 

but it showing  "unknown file size"

 

please help me.

It looks like the file path you are providing isn't accessible/valid. Try the following to see if the error is triggered (made some other improvements)

 

function formatbytes($file, $type) 
{
    $filesizeBytes = filesize($file);
    if(!$filesizeBytes)
    {
        echo "Unable to access file '{$file}'";
        return false;
    }

    switch($type)
    {
        case "GB":
            $filesize = $filesizeBytes * pow(.0009765625, 3) // bytes to GB 
            break;
        case "MB":
            $filesize = $filesizeBytes * pow(.0009765625, 2) // bytes to MB 
            break;
        case "KB":
        default:
            $type = 'KB';
            $filesize = $filesizeBytes * pow(.0009765625, 1) // bytes to KB 
            break; 
    }
    return round($filesize, 2).' '.$type;
}

echo formatbytes($file, 'KB');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.