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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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