Jump to content

counting the number of txt extension files in a directory.


ted_chou12

Recommended Posts

I want to count the number of txt extension files in a directory, this should be pretty straight forward with glob() but my host doesnt allow me to use that function, so im getting a bit stuck.
can anyone help me with this?
thanks a lot!
Ted.
put them into an array then count the array
[code]<?php
$files = array();
if ($handle = opendir('pdffiles/')) {
  while (false !== ($file = readdir($handle))) {
    if($file != "." && $file != ".."){
      if(strrchr($file, ".") == ".txt"){
      $files[] = $file;
      }
    }
  }
closedir($handle);
}
echo count($files);
?>[/code]

Ray
Ive made this into a function, but why doesnt it work, it always appears 0, but it works fine without the funciton
function threadcount($dir){
$files = array();
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
    if($file != "." && $file != ".."){
      if(strrchr($file, ".") == ".txt"){
      $files[] = $file;
      }
    }
  }
closedir($handle);
}
echo count($files);}
should just return the value rather than echo it out in the function. works both ways i guess just preference
[code]<?php
function threadcount($dir){
$files = array();
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
    if($file != "." && $file != ".."){
      if(strrchr($file, ".") == ".txt"){
      $files[] = $file;
      }
    }
  }
closedir($handle);
}
return count($files);
}
echo threadcount($folder);
?>[/code]

Ray
When you make it a function, the function has to return something back to the calling procedure so you can use it. In your case you want to return the array:
[code]<?php
function threadcount($dir){
    $files = array();
    if ($handle = opendir($dir)) {
        while (false !== ($file = readdir($handle))) {
              if($file != "." && $file != "..")
                  if(strrchr($file, ".") == ".txt")
                    $files[] = $file;
          }
      closedir($handle);
    }
    return ($files);
}
echo threadcount('.');
?>[/code]

Ken
yes you have to have the function established in order tou use it. What alot of developers do is create a file with all their functions in it. maybe call it function.inc.php. Now at the top of your page you require it so that the page stays cleaner.

[code]require('functions.inc.php');[/code]
Now all the functions you have written will be available to use.

Ray
[quote]do you always have to place it before you use it?[/quote]

No. You could have all your functions at the bottom of the page if you wanted, php doesn't care as long as the function is within scope (which is what I think craygo is talking about).
Thorpe got there first, but I was just going to say that this:

[code]<?php
$number = 1;

function doubleup($num){
  return $num*2;
}

echo doubleup($number);
?>[/code]

Produces the same result as this:

[code]<?php
$number = 1;

echo doubleup($number);

function doubleup($num){
  return $num*2;
}
?>[/code]

Regards
Huggie

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.