Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP counting files in a dir

Posted on 2003-03-04
9
Medium Priority
?
162 Views
Last Modified: 2006-11-17
Hi,

i am curious if anyone knows if PHP has a built in function that will read how many files are in a particular directory, and return that number.

I reailze the simplicity of this task but I am feeling particularly lazy today...

Thanks

-L
0
Comment
Question by:Langdon
  • 4
  • 4
9 Comments
 
LVL 15

Accepted Solution

by:
VGR earned 100 total points
ID: 8067495
no predefined function
0
 
LVL 1

Expert Comment

by:Jonza
ID: 8069807
<?php
countFiles($dir){;
  $cwd = getcwd();
  chdir($dir);
  $dh = opendir(".") or die("Could not open directory $dir");
  $num = 0;

  while($file = (readdir($dh) != false)){
    if($file != "." && $file != ".." && is_file($file)){
      $num++;
    }
  }//while

  return $num;
}//countFiles()

print(countFiles("info"));
?>

that should do it =)
0
 
LVL 1

Expert Comment

by:Jonza
ID: 8069813
oopz error at line 2:
countFiles(){ without that ;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Expert Comment

by:VGR
ID: 8070228
you forgot what you apparently planned :
at the end before the return $num  :
chdir($cwd); // to go back where we were
0
 
LVL 1

Expert Comment

by:Jonza
ID: 8070236
yup
0
 
LVL 15

Expert Comment

by:VGR
ID: 8070259
ah, and your style is "C-like" :D
Also, testing is_file($file) is enough to get rid of '.' and '..', no ??? :D :D :D
0
 
LVL 1

Expert Comment

by:Jonza
ID: 8070273
:D

gotta be sure!
Some systems may say that dirs are files..
depends on configuration.
0
 

Author Comment

by:Langdon
ID: 8073766
Here is what I ended up doing...

if(is_dir("images/".$listing_id))
     {
          $num_files = 0;
          clearstatcache();
          $dir = opendir("images/".$listing_id);
          while($file = readdir($dir))
               {
                    if(($file != ".") && ($file != "..") && (is_file($file))
                         {
                              $num_files++;
                         }
               }
     }

This works great... I was having problems at first and I didn't know where the . and .. were coming from. After some trial and error I figured it out.

But since VGR was the first person to answer the original question for the post "does php have a built in function for this task"
He gets the points.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8074128
please share the points 8-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question