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.

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.