Click to See Complete Forum and Search --> : File Count
Hey all,
I'm looking for a script that counts the files I have in certain directories and prints the output on a webpage. Anyone know of such script or care to share their knowlegde?
the-FoX
10-27-2003, 03:49 AM
many solutions lead to your aim:
1) you can open the directory recursively with the opendir() command and count the files
2) you can use the ls command under linux or dir under windows to list all files. combinded with wc-command (word count). or send the output to a file, load the outputfile with $arraydata=file('outputfile.txt'); into an array and show get the lines by: echo count($arraydata);
i would prefer to use commands from the operating system, cause they would be much faster than open every directory with php
Lotus
10-27-2003, 06:49 AM
I think I've seen a CGI script do this...
cgi.resourceindex.com (http://cgi.resourceindex.com)
They have everything, so they probably have this one too.
Try this:
<?PHP
$dir = "temp";
$handle = opendir($dir);
$num = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href=\"$dir/$file\">$file</a><br>";
$num++;
}
}
echo $num." files total";
closedir($handle);
?>
Thanks guys. Pyro's script is partially what I'm looking for. However, the other script I'm looking for is one that counts files according to extention in subdirectories. I've acquired this script (forgot from where):
<?php
$i=0;
function getDirList ($dirName) {
global $i;
$d = dir($dirName);
while($entry = $d->read()) {
if ($entry != "." && $entry != "..") {
if (is_dir($dirName."/".$entry)) {
getDirList($dirName."/".$entry);
} else if(substr($entry, -4)=='.jpg') {
if($read_file = file($dirName.'/'.$entry))
foreach($read_file as $line)
if(($line!="\n") && (substr($line, 0, 2)!="//"))
$i++;
echo $dirName."/".$entry."\n";
}
}
}
$d->close();
}
?>
It pretty much looks for only .jpg files (or other files depending on what I enter) in subdirectories. Any clue on how to count those files either with this script or another script? Thanks!
Nevermind... I did my own little tweaking and got the script to work. Go me! :D