Home arrow PHP arrow Page 9 - File And Directory Manipulation In PHP (part 2)

A Pattern Emerges - PHP

Now that you know the basics of reading and writing files, this second segment of our tutorial on the PHP filesystem API takes you into deeper waters, showing you how to copy, delete and rename files; scan directories; work with uploaded files over HTTP; perform pattern matches on file names; and read and write to processes instead of files.

TABLE OF CONTENTS:
  1. File And Directory Manipulation In PHP (part 2)
  2. Stripping It To The Bone
  3. Fertile Fields
  4. Configuring The System
  5. The Right Path
  6. Move It
  7. Beam Me Up
  8. Diving Into Directories
  9. A Pattern Emerges
  10. Purging The Dead
  11. Size Does Matter
  12. In Process
  13. Disk Full
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 65
August 21, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: