PHP: Some Ways to Scan a Directory
Posted on December 12, 2008, Filled under PHP,
Bookmark it
This is a list with some methods to retrieve the contents from a directory (folder). The following functions show both the files & the folders located in a specific directory.
scandir()
array scandir ( string $directory [, int $sorting_order [, resource $context]] )
This function only works in PHP5+. It returns an array of files and folders from the specified path. Here’s an usage example:
<?php $dir = '/some_files'; $files1 = scandir($dir); // sort in alphabetical order (Ascending) $files2 = scandir($dir, 1); // sort in alphabetical order (Descending) echo "<pre>"; print_r($files1); echo "</pre>"; echo "<pre>"; print_r($files2); echo "</pre>"; ?>
The example above would result in something like:
Array
(
[0] => .
[1] => ..
[2] => index.php
[3] => text_file.txt
[4] => images
)
Array
(
[0] => images
[1] => text_file.txt
[2] => index.php
[3] => ..
[4] => .
)
opendir(), while() & readdir()
This is the classical way to scan a directory in PHP. It’s an altternative to scandir() if you are using PHP4. Example:
<?php
$dir = "/images";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
>
To sort the resulted array you can use either sort() (elements will be arranged from lowest to highest) or rsort() (elements will be arranged from highest to lowest).
glob()
array glob ( string $pattern [, int $flags] )
This function finds pathnames matching a specific pattern. To get all files from a directory we’ll use *. It’s a convenient way to replace opendir() and friends
<?php
// get files from current directory
foreach (glob("*") as $filename) {
echo $filename; echo "<br />";
}
// get files from 'images'
foreach (glob("images/*") as $filename) {
echo $filename; echo "<br />";
}
// get files from 'documents' that end with .doc
foreach (glob("documents/*.doc") as $filename) {
echo $filename; echo "<br />";
}
?>
The find out more about these functions, consider checking the PHP Documentation.
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- December 12, 2008
- article by Gabriel C.
- 4 comments
Sponsors
Related Posts
-
PHP: Sort Files from Directory & Order them by Filemtime()at October 5, 2008 with 5 comments
-
How to recursively read a directory with all its contentat September 4, 2008
-
PHP: Calculate the Size, Number of Files & Folders of a Directoryat September 24, 2008 with 3 comments
-
How to Remove a (non-empty) Directory using PHPat September 28, 2008 with 1 comment
-
Show random image(s) from a directoryat September 21, 2008 with 3 comments

4 Replies to "PHP: Some Ways to Scan a Directory"
December 13, 2008 at 6:39 AM
foreach (glob("images/*") as $filename) {
echo $filename; echo "<br>";
}
It could be :
foreach (glob("images/*") as $filename) {
echo $filename."<br>";
}
December 26, 2008 at 3:32 PM
Of course. I've focused on the 'scan directory' methods
February 26, 2009 at 11:42 AM
On my real estate webisite I have trouble to get file name separate from extension.
February 26, 2009 at 9:57 PM
Consider checking the following post:
How to remove an extension from a filename