PHP Count files in dir

Dec 10, 2008 Author: City Hall

How to count files in one directorium with php

We know that in linux hiden files have dot (.filename)

also we have system dirs

.(curent)

..(main dir)

filename

.filename2

with simple code for counting files:

$count = 0;
$hd = opendir("C:\\xampp\\htdocs\\test\\");
while(($file = readdir($hd)) !== false) {
$count++;
}
echo $count;

count result will be 4 files .

We need to eliminate files who don't need us.  In our case don't  need to count  hiden files and file system direcotories.

This is the simple code for filtering.

 

<?php
$count = 0;
$hd = opendir("C:\\xampp\\htdocs\\test\\");
while(($file = readdir($hd)) !== false) {


if ($file[0] != "."){
$count++;}
}


echo $count;
?>

count result will be 1 files

That's all more ideas next time

tags: php count files dir

views 4493
  1. Add New Comment