469,328 Members | 1,279 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

How to limit/filter scandir() response

Claus Mygind
571 512MB
I want to return the content (list of files) in a folder to an array. In my dBase program I can filter the content by adding a skeleton file description like this *.pdf

Is there a way to limit/filter the response when using the php command: $files = scandir($dir);

I see in the manual something called $context as in
array scandir ( string $directory [, int $sorting_order = 0 [, resource $context ]] )

When researching that, the manual got a little fuzzy for me.

Could any one complete this line for me

$files = scandir($dir, 1 [, resource $context ] )

where it would only return any .pdf file?
May 16 '11 #1

✓ answered by Claus Mygind

If no better answer exist, I guess I will have to go with these extra steps to accomplish the same job of filtering the data.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (is_dir($cDir)) {
  3.    $files2 = scandir($cDir);
  4.    foreach ($files2 as &$value)
  5.    {
  6.       $parts = explode(".", $value);
  7.       if (strtoupper(end($parts)) == "PDF") {
  8.          echo $value. "<br/>";
  9.       }
  10.    }
  11.    unset($value);
  12. }
  13. ?>
It seems a bit much code for such a small task. Hope there is something better.

7 10656
Claus Mygind
571 512MB
am I even on the right track with something like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. $params['paramname'] = ".pdf";
  3. $files2 = scandir($cDir,1,stream_context_set_params( $cDir , $params['paramname'] ));
May 16 '11 #2
Claus Mygind
571 512MB
If no better answer exist, I guess I will have to go with these extra steps to accomplish the same job of filtering the data.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (is_dir($cDir)) {
  3.    $files2 = scandir($cDir);
  4.    foreach ($files2 as &$value)
  5.    {
  6.       $parts = explode(".", $value);
  7.       if (strtoupper(end($parts)) == "PDF") {
  8.          echo $value. "<br/>";
  9.       }
  10.    }
  11.    unset($value);
  12. }
  13. ?>
It seems a bit much code for such a small task. Hope there is something better.
May 16 '11 #3
Dormilich
8,651 Expert Mod 8TB
look into GlobIterator, thatís made for something like that.
May 17 '11 #4
Claus Mygind
571 512MB
Thank you for the reference. At this time the manual is beyond my comprehension for this command.

I did try the link to glob() and that will cut out the one if statement to test for file type.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. foreach (glob("*.txt") as $filename) {
  3.     echo "$filename size " . filesize($filename) . "\n";
  4. }
  5. ?>
  6.  
May 20 '11 #5
Dormilich
8,651 Expert Mod 8TB
the GlobIterator code doesnít look much different:
Expand|Select|Wrap|Line Numbers
  1. foreach (new GlobIterator("*.txt") as $file)
  2. {
  3.     echo $file, " size ", $item->getSize(), PHP_EOL;
  4. }
May 20 '11 #6
Claus Mygind
571 512MB
Thanks for that explanation. Why can't they put something that simple in the manual for easy comprehension.
May 23 '11 #7
Dormilich
8,651 Expert Mod 8TB
this is the example from the manual slightly rewritten for your problem (though the example is listed under GlobIterator::__construct())

but yes, SPL is way underdocumented
May 23 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Salad | last post: by
reply views Thread by Thomas Satzinger | last post: by
2 posts views Thread by Renť Kšlin | last post: by
1 post views Thread by ocbka1 | last post: by
1 post views Thread by Josh Naro | last post: by
7 posts views Thread by Sheldon | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.