How to count folder files using php

Please support our PHP advertiser: 50% off 6 Months Dedicated Server Hosting from 1&1!
Reply

Join Date: Jul 2006
Posts: 197
Reputation: vssp has a little shameless behaviour in the past 
Solved Threads: 5
vssp vssp is offline Offline
Junior Poster

How to count folder files using php

 
0
  #1
Oct 24th, 2006
hai friends

I need to count the files with in the selected folder.

How to count the files ?? please help me. If any paossibel ple4ase send me sample code.

Thanks
vssp
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,110
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 68
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to count folder files using php

 
0
  #2
Oct 24th, 2006
Originally Posted by vssp View Post
hai friends

I need to count the files with in the selected folder.

How to count the files ?? please help me. If any paossibel ple4ase send me sample code.

Thanks
vssp
I wrote a directory reading class in my blog that you could look at:
http://fijiwebdesign.com/content/view/83/77/

Heres how you would count the number of files in a directory using the class in the link above:

[PHP]/**
* Use the readDir class to count files in the directory
*/

$dir_path = '/my/directory/path'; // change to the directory path

$dir = new readDir(); // instantiate our class
$file_count = 0;

// set the directory to read
$dir->setPath( $dir_path );

// we do not want to read sub folders
$dir->readRecursive(false);

// set a function to call when a new dir is read
$dir->setEvent( 'readDir_file', 'increment_file_count' );

// read the dir
if ( !$dir->read() ) {
die($dir->error());
}

// our custom function called when a new file is read
function increment_file_count($path, $filename) {
global $file_count;

$file_count++; // increment file count

}

echo $file_count; // this is the number of files in the directory $dir_path [/PHP]

Should be quite self explanatory.

If you'd rather go with using the regular php functions, heres an example:

[PHP]/**
* Count files or sub-directories in a directory
* @author digital-ether at fijiwebdesign.com
*
* @param string directory path
* @param string element type to count (file|dir)
*/
function count_dir_elements($dir_path, $type = 'file') {
$file_count = 0;
$dir_count = 0;
if ($dh = opendir($dir_path)) {
$i = 0;
while ($el = readdir($dh)) {
$path = $dir.'/'.$el;

if (is_dir($path) && $el != '.' && $el != '..') {
$dir_count++;
} elseif (is_file($path)) {
$file_count++;
}
$i++;
}
closedir($dh);
} else {
return false;
}
return $type == 'file' ? $file_count : $dir_count;
}

// eg: count the files in this directory

echo count_dir_elements(dirname(__FILE__), $type = 'file');[/PHP]
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 197
Reputation: vssp has a little shameless behaviour in the past 
Solved Threads: 5
vssp vssp is offline Offline
Junior Poster

Re: How to count folder files using php

 
0
  #3
Oct 25th, 2006
Tanks for ur reply I check the code and let u know

vssp
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 197
Reputation: vssp has a little shameless behaviour in the past 
Solved Threads: 5
vssp vssp is offline Offline
Junior Poster

Re: How to count folder files using php

 
0
  #4
Oct 25th, 2006
Thanks its working fine
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 20110 | Replies: 3
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC