count files in a directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    count files in a directory

    Hello, Using PHP 4, is there a fucntion that counts the files in a
    given directory? You can assume there are no sub-directories within
    this directory and hence, no files within the sub-directories.

    Thanks for any functions or one-liners. - Dave

  • Peter van Schie

    #2
    Re: count files in a directory

    laredotornado@z ipmail.com schreef:
    Hello, Using PHP 4, is there a fucntion that counts the files in a
    given directory? You can assume there are no sub-directories within
    this directory and hence, no files within the sub-directories.
    >
    Thanks for any functions or one-liners. - Dave
    Hi Dave,

    You could use readdir to accomplish this.
    Example:

    <?php
    function countFiles($str DirName)
    {
    if ($hndDir = opendir($strDir Name))
    {
    $intCount = 0;
    while (false !== ($strFilename = readdir($hndDir )))
    {
    if ($strFilename != "." && $strFilename != "..")
    {
    $intCount++;
    }
    }
    closedir($hndDi r);
    }
    else
    {
    $intCount = -1;
    }

    return $intCount;
    }

    echo countFiles("C:\ \Temp");
    ?>

    HTH.
    Peter.

    --

    Comment

    • Krustov

      #3
      Re: count files in a directory

      <comp.lang.ph p>
      <laredotornado@ zipmail.com>
      <23 Jul 2006 06:48:02 -0700>
      <1153662482.561 797.42310@75g20 00cwc.googlegro ups.com>
      Hello, Using PHP 4, is there a fucntion that counts the files in a
      given directory? You can assume there are no sub-directories within
      this directory and hence, no files within the sub-directories.
      >
      Thanks for any functions or one-liners.
      >
      <?php

      $dirname="demo" ;
      $dh=opendir($di rname);
      while ($dave=readdir( $dh))
      {
      print "$dave <br>";
      }
      closedir ($dh);

      ?>


      --
      Encrypted email address

      Make a shorter url

      Comment

      • Krustov

        #4
        Re: count files in a directory

        <comp.lang.ph p>
        <Krustov>
        <Sun, 23 Jul 2006 15:54:51 +0100>
        <MPG.1f2d9e111c 17bae989c86@new s.newsreader.co m>
        <?php
        >
        $dirname="demo" ;
        $dh=opendir($di rname);
        while ($dave=readdir( $dh))
        {
        print "$dave <br>";
        }
        closedir ($dh);
        >
        ?>
        >
        I missed the bit where you said count .

        <?php

        $cnt=0;
        $dirname="demo" ;
        $dh = opendir($dirnam e);
        while ($dave=readdir( $dh))
        {
        print "$dave <br>";
        $cnt=$cnt+1;
        }
        closedir ($dh);

        print $cnt-2;

        ?>


        --
        Encrypted email address

        Make a shorter url

        Comment

        • Chung Leong

          #5
          Re: count files in a directory


          laredotornado@z ipmail.com wrote:
          Hello, Using PHP 4, is there a fucntion that counts the files in a
          given directory? You can assume there are no sub-directories within
          this directory and hence, no files within the sub-directories.
          >
          Thanks for any functions or one-liners. - Dave
          count(glob("$di r/*.*"));

          Comment

          Working...