Filesize script

software development


I’m working on a script to check the size of a directory of files, but I’m getting nothing returned. There are mp3 files in a directory that should be giving me filesizes bigger than 0, but I’m returning warnings followed by 0 MB when I run this:


ini_set (‘display_errors’, 1);
error_reporting (E_ALL);
$dir = ‘…/revisited’;
$files = scandir ($dir);

foreach ($files as $file) {
print $file;
print “\n”;
$fs = filesize ($file);
$fs /= pow (1024, 2);
echo round ($fs, 1) . " MB\n";

/* $files = opendir ($dir);

while ($file = readdir ($files)) {
if (file_exists ($file) && filesize($file) > 1000000) {
print $file;
print “\n”;
$fs = filesize ($file);
$fs /= pow (1024, 2);
echo round ($fs, 1) . " MB\n";
closedir ($files);


what’s the error? I have a shell script around here somewhere that can do this. I’ll start hunting for it.


Oh I missed it the first time. It’s not returning a value of 0 MBs, it’s returning a boolean false because you’re not using a valid path. You have to specify a directory path like /home/username/directory.

Here’s the shell scripts anyway…

Return the number of files of a certain file extension:
find . -name ‘*.mp3’ | egrep “mp3” -c

Return the total disk usage by a certain file extension, in MBs:
find . -name ‘*.mp3’ -type f -exec du -m {} ;|awk ‘{ s = s+$1 } END { print "Total MB used: ",s }’


The filepath I’m using is simply a test path on my local disc. Will that affect the overall outcome?


Portions of this thread might be useful: Phrase&Searchpage=0&Limit=25&Old=6months&Main=62223


This problem has been solved sorta. It seems my local install of PHP does not handle filsize() properly, but the script works on the server.