Sajesh Mohan Posted May 20, 2012 Share Posted May 20, 2012 $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. Quote Link to comment https://forums.phpfreaks.com/topic/262812-how-to-get-the-size-of-a-file-from-directory/ Share on other sites More sharing options...
wigwambam Posted May 20, 2012 Share Posted May 20, 2012 Should this line: $size = filesize($path); // not working not be? $size = filesize($file); Quote Link to comment https://forums.phpfreaks.com/topic/262812-how-to-get-the-size-of-a-file-from-directory/#findComment-1346970 Share on other sites More sharing options...
Sajesh Mohan Posted May 20, 2012 Author Share Posted May 20, 2012 same Quote Link to comment https://forums.phpfreaks.com/topic/262812-how-to-get-the-size-of-a-file-from-directory/#findComment-1346975 Share on other sites More sharing options...
Psycho Posted May 20, 2012 Share Posted May 20, 2012 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'); Quote Link to comment https://forums.phpfreaks.com/topic/262812-how-to-get-the-size-of-a-file-from-directory/#findComment-1346979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.