Filesize script

software development

#1

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:

[code]<?php

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);
*/
?>
[/code]


#2

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


#3

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.

http://us2.php.net/scandir

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 }’


#4

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


#5

Portions of this thread might be useful: http://discussion.dreamhost.com/showthreaded.pl?Cat=&Board=forum_multimedia&Number=62349&Search=true&Forum=All_Forums&Words=opendir&Match=Entire Phrase&Searchpage=0&Limit=25&Old=6months&Main=62223


#6

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.