473,561 Members | 3,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to limit/filter scandir() response

Claus Mygind
571 Contributor
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
7 12043
Claus Mygind
571 Contributor
am I even on the right track with something like this

Expand|Select|Wrap|Line Numbers
  2. $params['paramname'] = ".pdf";
  3. $files2 = scandir($cDir,1,stream_context_set_params( $cDir , $params['paramname'] ));
May 16 '11 #2
Claus Mygind
571 Contributor
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
8,658 Recognized Expert Moderator Expert
look into GlobIterator, thatís made for something like that.
May 17 '11 #4
Claus Mygind
571 Contributor
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. ?>
May 20 '11 #5
8,658 Recognized Expert Moderator Expert
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 Contributor
Thanks for that explanation. Why can't they put something that simple in the manual for easy comprehension.
May 23 '11 #7
8,658 Recognized Expert Moderator Expert
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

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 log. At first I was only going to present the record the user selected. Then I thought, what the heck, give the user all of them but start out at the record the user is currently on. If I...
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 (does not contain member property), or 0. Using LDIFDE as a comparison I get the same results. No members means just that, an empty group. Zero means that the DirectorySearcher.SizeLimit has been...
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 contents _doc.PreserveWhitespace = true; byte _bArr;
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 $dir; echo $tmp; foreach ($tmp as $key => $datei) {
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 filter class to write it out. the problems start happening when i redirect - i need to somehow end the response but not with Response.End because it doesn't allow it to get to the Redirect statement...
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 output stream from the Response object. I've tried attaching a custom filter to Response.Filter, but the custom filter receives the stream in separate chunks. I need the entire stream at once in order...
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 output stream from the Response object. I've tried attaching a custom filter to Response.Filter, but the custom filter receives the stream in separate chunks. I need the entire stream at once in order...
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() that scans the directory and return the desired file names after filtering. I have used the function file_select() to filter out unwanted files and everything works nicely except that I keep getting...
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., http://bytes.com/topic/access/answers/702416-search-code-how-filter-using-code-sql), nothing has addressed my problem, so Iím going to have to post. :) Iím using Access 2007. I have a form with a sub-form. I want to filter for text in the...
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") ) { $Id=$q->param('User_Id'); $Name=$q->param('User_Name'); if ($Id ne "" ) { $filterexpression= $filterexpression." UserId like '"...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.