PHP
  Home arrow PHP arrow Page 3 - Finding Paths, Timestamps and More wit...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-09-18

    Table of Contents:
  • Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
  • Learning the rewind(), current() and valid() methods
  • Using the getFileName() and isFile() methods
  • Using the getMTime(), getATime(), and getCTime() methods
  • Using the getPath() and getPathName() methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP - Using the getFileName() and isFile() methods


    (Page 3 of 5 )

    As I mentioned at the end of the previous section, the “DirectoryIterator” class has many more methods that can make your life much easier when it comes to processing directories and their corresponding files. In this particular situation, I’ll show you how to use two entirely new methods that belong to this class: the “getFile()” and “isFile()” methods respectively.

    In most cases, a hands-on example can be much more instructive than some boring sentences. So the use of the methods that I mentioned before is illustrated by the following script. Take a look:

    $dirProc=new DirectoryIterator('default_path/');
    foreach($dirProc as $dirContent){
        if($dirContent->isFile()){
            echo 'Name of current file is : '.$dirContent-
    >getFileName().'<br />';
        }
    }

    In this case, the example above first loops over the selected directory and checks for the files included in it by using the corresponding “isFile()” method (by the way, this is very similar to the “is_file()” PHP native function, isn’t it?), and finally displays their names by the “getFileName()” method.

    Now, after examining the logic of the previous script, and assuming that the selected “default_path” contains two files, “file1.txt” and “file2.txt” respectively, have a look at the output generated by it:

    Name of current file is : file1.txt
    Name of current file is : file2.txt

    As shown in the example you learned before, retrieving the names of the files that might be included within a specific directory is really a straightforward process that doesn’t bear a longer discussion. However, this journey isn’t over yet, since the “DirectoryIterator” class also comes packaged with an excellent set of methods, specifically to deal with timestamps of directories and files.

    That’s exactly the subject that I’ll discuss in the upcoming section, which certainly is a good reason for you to keep on reading. To learn more about these new methods, please click on the link below.

    More PHP Articles
    More By Alejandro Gervasio


       · Over this second part of the series, you'll learn a few more useful methods that are...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT