PHP
  Home arrow PHP arrow Page 9 - File And Directory Manipulation In PHP...
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

File And Directory Manipulation In PHP (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 59
    2003-08-21

    Table of Contents:
  • File And Directory Manipulation In PHP (part 2)
  • Stripping It To The Bone
  • Fertile Fields
  • Configuring The System
  • The Right Path
  • Move It
  • Beam Me Up
  • Diving Into Directories
  • A Pattern Emerges
  • Purging The Dead
  • Size Does Matter
  • In Process
  • Disk Full

  • 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

    Minimize the cost of deploying database applications. Advantage Database Server or Microsoft SQL Server – Which One is Right for You? Learn now!

    File And Directory Manipulation In PHP (part 2) - A Pattern Emerges
    (Page 9 of 13 )

    Want to list just the files matching a specific pattern? Use the neat little fnmatch() function, new in PHP 4.3, which matches strings against wildcard patterns. Here's a quick example:


    <?php

    // set directory name
    $dir = "/bin";

    // set pattern
    $pattern = "e*";

    // open directory and parse file list
    if (is_dir($dir))
    {
    if ($dh = opendir($dir))
    {
    // iterate over file list
    while (($filename = readdir($dh)) !== false)
    {
    // if filename matches search pattern, print it
    if (fnmatch($pattern, $filename))
    {
    echo $dir . "/" . $filename . "\n";
    }
    }
    // close directory
    closedir($dh);
    }
    }

    ?>

    Here's an example of the output:


    /bin/ed
    /bin/egrep
    /bin/echo
    /bin/env
    /bin/ex

    The * wildcard matches one or more characters; if this is not what you want, you can also use the ? wildcard to match a single character.

    An alternative to using fnmatch() with the opendir() and readdir() functions is the glob() function, also new to PHP 4.3 - this function searches the current directory for files matching the specified pattern and returns them as an array. Consider the following rewrite of the example above, which demonstrates:


    <?php

    // set directory name
    $dir = "/bin";

    // set pattern
    $pattern = "e*";

    // change to named directory
    chdir($dir);

    // find files matching pattern
    $files = glob($pattern);

    // iterate over files array and print filenames
    foreach ($files as $f)
    {
    echo $dir . "/" . $f . "\r\n";
    }

    ?>

    Since glob() looks in the current directory for files, remember to always
    chdir() to the correct directory before executing glob().

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway