| | |
How to count folder files using php
![]() |
•
•
•
•
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
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Open In New Window Php (PHP)
- Move folder in Mila function using PHP (PHP)
- How to write the new line existing pdf file using php (PHP)
- Disable PHP in specified folder? (PHP)
- How do I open .mif files with PHP (PHP)
- DB-Authentication php/mysql on Mac OSX v3 (PHP)
Other Threads in the PHP Forum
- Previous Thread: Vbulletin item
- Next Thread: Basic stuff but I'm stuck!!
Views: 20110 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array box buttons cakephp check checkbox class cms code combobox database date directory display download dropdown drupal dynamic echo email error file files form forms functions header hosting href htaccess html image include insert integration ip java javascript joomla jquery limit link list login loop mail menu methods mlm mod_rewrite multiple mysql onclick order parse password paypal pdf php post problem query radio random recursion redirect regex remote rewrite rows script search select server session sort source sql storage string structure table update upload url user validate validation variable video web website wordpress xml zend






