?
Solved

PHP counting files in a dir

Posted on 2003-03-04
9
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Industry Leaders: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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