Jump to content

Recommended Posts

Hi

 

I'm working with the following code to count the number of files in a directory, however, it doesn't seem to count files in subdirectories, does anyone have any ideas how I can get it to?

 

Thanks!

 

The code so far is as follows:

 

<?php

 

function numFilesInDir($directory, $includeDirs = false)

{

  $files = glob($directory.'/*');

  if($files === false)

  {

      user_error(__FUNCTION__."(): Invalid directory ($directory)");

      return false;

  }

  $numFiles = count($files);

  if(!$includeDirs)      //remove ! to count folders instead of files

  {

      $dirs = glob($directory.'/*', GLOB_ONLYDIR);

      $numFiles = $numFiles - count($dirs);

  }

  return $numFiles;

}

$numFiles = numFilesInDir('../media/Images');

if($numFiles === false)

{

  echo "<p>Oops....something went wrong.</p>\n";

}

else

{

  echo "<p>There are $numFiles pictures in the Image folder.</p>\n";

}

?>

Hi, thanks very much for your quick reply and whilst I'm sure to someone with a good understanding of php it's all that's needed, however I'm a real beginner, could you (or someone) explain exactly how I'd go about calling the function again please.

 

Many thanks in advance

Right now your function simply uses glob() to get a list of ALL the objects in a folder. So, your function is returning the number of files and folders in the directory. So, you will need to iterate over each object returned in glob and increment the file count if it is a file. If an object is a directory, then you call the function again using that directory. That is what is meant by being recursive.

 

Instead of making the function overly complex based upon whether you want to get the count of files, folder or both; I rewrote it to always return both plus the total. Then you can decide which vlaue to display as needed. The second parameter is to specify if you want to count the objects in the subfolders (i.e. be recursive)

function directoryCount($directory, $recursive=false)
{
    $objects = glob($directory.'/*');
    if($objects === false)
    {
        user_error(__FUNCTION__."(): Invalid directory ($directory)");
        return false;
    }
    $fileCount = 0;
    $folderCount = 0;
    foreach($objects as $object)
    {
        (is_file($object)) ? $fileCount++ : $folderCount++;
        if($recursive)
        {
            $subFolderCount = directoryCount($object, $recursive);
            $fileCount += $subFolderCount->files;
            $folderCount += $subFolderCount->folders;
        }
    }
    $output->files   = $fileCount;
    $output->folders = $folderCount;
    $output->total   = ($fileCount+$folderCount);
    return $output;
}

$folderCount = directoryCount('images', true);
if($folderCount === false)
{
   echo "<p>Oops....something went wrong.</p>\n";
}
else
{
   echo "<p>There are {$folderCount->files} files and {$folderCount->folders} folders in the Image folder. That is a total of {$folderCount->total}</p>\n";
} 

Awesome, thanks very much for your reply, however, it doesn't seem to been totalling the correct number of files in the folder, below is the code that you (with the path) re did for me but in the eg i did it counts 33 files when there are actually 35 (with no subfolders), any ideas why?

 

<?php

 

function directoryCount($directory, $recursive=false)

{

    $objects = glob($directory.'/*');

    if($objects === false)

    {

        user_error(__FUNCTION__."(): Invalid directory ($directory)");

        return false;

    }

    $fileCount = 0;

    $folderCount = 0;

    foreach($objects as $object)

    {

        (is_file($object)) ? $fileCount++ : $folderCount++;

        if($recursive)

        {

            $subFolderCount = directoryCount($object, $recursive);

            $fileCount += $subFolderCount->files;

            $folderCount += $subFolderCount->folders;

        }

    }

    $output->files  = $fileCount;

    $output->folders = $folderCount;

    $output->total  = ($fileCount+$folderCount);

    return $output;

}

 

$folderCount = directoryCount('../media/Images/A', true);

if($folderCount === false)

{

  echo "<p>Oops....something went wrong.</p>\n";

}

else

{

  echo "<p>There are {$folderCount->files} images beginning with the letter A. </p>\n"; // That is a total of {$folderCount->total} -- and {$folderCount->folders} folders --

}

?>

It works perfectly fine for me. Are you sure you counted the files and folders correctly?

 

You could add some debugging info to see what it is actually counting. Add the debugging line as shown here

    {
        (is_file($object)) ? $fileCount++ : $folderCount++;
//Debugging
echo "{$object} : " . ((is_file($object)) ? 'FILE' : 'FOLDER' ) . "<br />\n";
        if($recursive)
        {

 

If you can identify a file or folder that is not being counted then we can determine what the problem is.

 

Hi, I'm afraid I didn't know how to insert the bug tracking script, I just get the following syntax error if i cut and paste it into the code. I'm a real beginner to php so please bear with me!  :-[

 

Parse error: syntax error, unexpected $end in /c/website/count.php on line 49

 

I think i can see what its doing though but i don't know why.  The actual total of folder "A" has 35 fies (I counted them manually) the script shows 33 files and 2 folders, when there aren't any folders in the A folder and all the file types are the same.

 

Any ideas why it doing this?

 

Thanks

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.