Jump to content

List # of files


Ben Phelps

Recommended Posts

I would look at this page http://ca.php.net/readdir to help you out. I'm a newb  but by looking at the sample code on that
page I would think it would look something like this


$handle = opendir('/path/to/files')

// loop over the directory.
  while (false !== ($file = readdir($handle))) {
      $file_array[].=$file
  }

I think this would make an array of everything in the  selected directory. Then all you would have to do it
use the count function on the array $file_num = count($file_array). I think that would give you the number
you are looking for.

Cheers!
Stephen
Link to comment
https://forums.phpfreaks.com/topic/26186-list-of-files/#findComment-119774
Share on other sites

with kelset's reply add in an if statement to check that the filename isn't '.' or '..' by going like this:
[code=php:0]
$dir = opendir('/dir/path');

while(false !== ($file = readdir($dir))){
if($file != "." && $file != ".."){
$file_array[] .= $file;
}
}

echo "Files in directory: ".count($file_array);
[/code]

kelset note that you didnt end your lines with ';'.
Link to comment
https://forums.phpfreaks.com/topic/26186-list-of-files/#findComment-119783
Share on other sites

I found 1, thanks for the help.  Kudos to all.  [img]http://www.onecompare.com/images/emotions/smiley.gif[/img]

[code]<?php
function CountDir($aDir, $aRecurse)
{
$Count = 0;

$d = dir($aDir);

while ($Entry = $d->Read())
{
if (!(($Entry == "..") || ($Entry == ".")))
{
if (Is_Dir($aDir . '/' . $Entry))
{
if ($aRecurse)
{
$Count += CountDir($aDir . '/' . $Entry, $aRecurse);
}
}
else
{
$Count++;
}
}
}

return $Count;
}

echo CountDir(files/dir, True);
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/26186-list-of-files/#findComment-119808
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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