Reply
Counting Files
Old 05-16-2007, 03:21 AM Counting Files
cbeaudin's Avatar
Super Talker

Posts: 126
Name: Clayton Beaudin
Location: Proud to be Canadian
Not sure if this is possible, but ill ask anyways.

I have a script with a variable "$totalfiles" and it contains the number of files in a specified directory (ie. if /root/folder1 contains 10 files, $totalfiles==10)

When i have been adding and deleting files from these dir's i have been manually changing the variable in the script accordingly.

Im looking for a script to count the number of files in a directory and automatically set $totalfiles to the correct number so that other people can add and delete files without having to change the script.

The script does not connect to a database.

It would be very appreciated if you could explain any code given so i can understand how it works and learn from it in the future. I understand your all busy so if you dont have time to explain any code i understand, im sure i can figure it out after some googling

Thanks in advance for all replies, it is greatly appreciated.
cbeaudin is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 05-16-2007, 04:01 AM Re: Counting Files
jito's Avatar
L

Posts: 550
Name: surajit ray
Location: inside the heart of my friends
Possible. just read a dir for files and increment a counter.
PHP Code:
$dir "path_to_the_dir";
$handle opendir($dir);
$count 0;
while (
false !== ($file readdir($handle))) 
{
if (
is_file($file) && $file !== '.' && $file !== '..'
{
$count++;
}
}
$count $count 1;
echo 
$count
__________________
Take care about what you think. Thoughts can travel far - Swami Vivekananda

Last edited by jito : 05-16-2007 at 04:06 AM. Reason: addition
jito is offline
Reply With Quote
View Public Profile
 
Old 05-16-2007, 04:13 AM Re: Counting Files
tripy's Avatar
Fetchez la vache!

Posts: 1,924
Name: Thierry
Location: In the void
Look onthe readdir() function explaination for an exemple:
http://www.php.net/manual/en/function.readdir.php
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-16-2007, 11:43 AM Re: Counting Files
mgraphic's Avatar
Truth Seeker

Posts: 2,305
Name: Keith Marshall
Location: West Hartford, CT
Here is a function I have worked up:

PHP Code:
<?php
  
  
// Use the following example:
  
echo 'Files Counted: ' count_files('/path/to/directory/');
  
  echo 
"<br />\n";
  
  
// Or use the following to not count files included in sub folders
  
echo 'Files Counted (root dir only): ' count_files('/path/to/directory/'false);
  
  
  
// Requires trailing slash in path string
  
function count_files($path$include_subdirectories true) {
    
$return 0;
    if (!
is_dir($path)) $return false;
    if (
$return === && $dir = @dir($path)) {
      while (
$file $dir->read()) {
        if (
$include_subdirectories && trim($file) != '.' && trim($file) != '..' && is_dir($path $file '/')) {
          
$return = (is_int($cf count_files($path $file '/'))) ? $return $cf $cf;
        }
        if (
is_file($path $file)) $return++;
      }
    }
    return 
is_int($return) ? $return 'An error has occurred with function count_files(), be sure to use trailing slash in path';
  }
  
?>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-17-2007, 02:29 AM Re: Counting Files
cbeaudin's Avatar
Super Talker

Posts: 126
Name: Clayton Beaudin
Location: Proud to be Canadian
Thank you for all replies, will be trying code tomorrow!
cbeaudin is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Counting Files
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12599 seconds with 13 queries