Here are a few ways you can list all the files in a specific directory. In the examples below, it is the same directory as where the PHP application is.

Using the PEAR library

To use the PEAR library to accomplish this, you need to make use of the File_Find class. There is a method search() that you can call statically to perform a search of a directory, which returns the results in an array.

Code:

<?php
require_once ‘File/Find.php’; // include the PEAR class

$files = File_Find::search(“\..+$”,getcwd()); // getcwd() prints the current working directory; the first parameter contains a “regular expression”
sort($files); // sorts the files in the array in alphabetical order

echo “<ul>”;
foreach($files as $file) echo “<li>” . basename(htmlspecialchars($file)) . “</li>”; // basename makes sure the returned string is not the absolute path of the file (so not this: /home/user/public_html/and/so/on)
echo “</ul>”;
?>

Using standard PHP functions

Using standard functions is just as easy:

<?php
$dir = opendir(getcwd()); // getcwd() prints the current working directory

echo “<ul>”;

while(($file = readdir($dir)) !== false) // the readdir() function advances to the next file in the resource ($dir) and the while loop cycles through each item
{
if(preg_match(“/\.[a-zA-Z0-9\-_]+$/”,$file)) // the first parameter contains a “regular expression”
{
echo “<li>” . $file . “</li>”;
}
}

echo “</ul>”;

closedir($dir);
?>

What are regular expressions?

Regular expressions are a powerful way to find matches in strings, as well as pattern matches. The most complicated part of using regular expressions are the use of “metacharacters”, which are characters which have a specialised purpose in regular expression syntax. Here is an explanation of the two regular expressions used above:

  • Note: When using preg_match(), regular expressions have to be within the forward-slash characters, so PHP knows where the start and end of a regular expression string is.
  • \. the backslash tells PHP to take the period character literally, because it is otherwise a reserved character (metacharacter). In the first regular expression (the code using the PEAR library), you’ll actually see two period characters – \.. – this is because I want the first character to be a full stop, then any character the . is a metacharacter which means “any character to match – a, b, c, f, z – etc.”.
  • [a-zA-Z0-9\-_] characters within these types of brackets are “character groups”, so to speak. So in this case, I want to match file names which have a period character in them, with any form of a-z, A-Z, 0-9, _ and – character in them. The backslash before the – is required in this context because it otherwise designates the range (e.g. A-Z, A-C, F-Z and so on).
  • + is a metacharacter which means “1 or more times”. In other words, I want at least one character to match after the period character (so, for example, a.jpg would be a match)
  • $ is a metacharacter which means “end of string” – in other words, I want .jpg to be a match only if it is at the end of the file name string
  • . is a metacharacter which means “any character” to match – a, b, c, f, z, 0, 1, 2 – and so on.

Hope this helps.

 

, , , , , , ,