Jump to content

counting the number of txt extension files in a directory.


ted_chou12

Recommended Posts

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
Link to comment
Share on other sites

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);}
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[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).
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.