472,352 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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 11647
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,658 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,658 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,658 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

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

Similar topics

2
by: Salad | last post by:
I have a log file with a list of records. The log file can be unfiltered or filtered. I have a command button to call a data entry form from the...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member...
0
by: Thomas Satzinger | last post by:
Hi to all. I am using the follwonmg code to download from an internal XmlDocument to the client file system: XmlDocument _doc ; // has some...
2
by: Renť Kšlin | last post by:
Hi! I've got a tiny little problem :-) with the following code: .... function get_styles($dir) { $tmp = scandir($dir); // Line 19 echo...
1
by: ocbka1 | last post by:
i'm using creating a webpage on the fly that i save as an xls file to be attached to an email and sent dynamically. i've got a custom response...
1
by: Josh Naro | last post by:
I am writing a module that requires the entire output from a web app to perform its function. So, basically I need to be able to pull the entire...
2
by: Josh Naro | last post by:
I am writing a module that requires the entire output from a web app to perform its function. So, basically I need to be able to pull the entire...
7
by: Sheldon | last post by:
Hi, I have a little warning that I am trying to understand but it escapes me. Perhaps someone can help here. I have the function read_files()...
4
by: kcropper | last post by:
Hi, Iíve been trolling these forums for a while now, and while Iíve found other related threads (e.g.,...
1
by: spyka | last post by:
I have issue regarding paging using filter expression. Here's the piece of code for filtering: if ( $q->param("Filter") ) {...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.