| 5 file.inc | file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $depth = 0) |
| 6 file.inc | file_scan_directory($dir, $mask, |
| 7 file.inc | file_scan_directory($dir, $mask, $options = array(), $depth = 0) |
| 8 file.inc | file_scan_directory($dir, $mask, $options = array(), $depth = 0) |
Finds all files that match a given mask in a given directory.
Directories and files beginning with a period are excluded; this prevents hidden files and directories (such as SVN working directories) from being scanned.
Parameters
$dir: The base directory for the scan, without trailing slash.
$mask: The regular expression of the files to find.
$nomask: An array of files/directories to ignore.
$callback: The callback function to call for each match.
$recurse: When TRUE, the directory scan will recurse the entire tree starting at the provided directory.
$key: The key to be used for the returned associative array of files. Possible values are "filename", for the path starting with $dir; "basename", for the basename of the file; and "name" for the name of the file without the extension.
$min_depth: Minimum depth of directories to return files from.
$depth: Current depth of recursion. This parameter is only used internally and should not be passed in.
Return value
An associative array (keyed on the provided key) of objects with "filename", "basename", and "name" members corresponding to the matching files.
Related topics
File
- includes/
file.inc, line 959 - API for handling file uploads and server file management.
Code
<?php
function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $depth = 0) {
$key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename');
$files = array();
if (is_dir($dir) && $handle = opendir($dir)) {
while (FALSE !== ($file = readdir($handle))) {
if (!in_array($file, $nomask) && $file[0] != '.') {
if (is_dir("$dir/$file") && $recurse) {
// Give priority to files in this folder by merging them in after any subdirectory files.
$files = array_merge(file_scan_directory("$dir/$file", $mask, $nomask, $callback, $recurse, $key, $min_depth, $depth + 1), $files);
}
elseif ($depth >= $min_depth && @ereg($mask, $file)) {
// Always use this match over anything already set in $files with the same $$key.
$filename = "$dir/$file";
$basename = basename($file);
$name = substr($basename, 0, strrpos($basename, '.'));
$files[$$key] = new stdClass();
$files[$$key]->filename = $filename;
$files[$$key]->basename = $basename;
$files[$$key]->name = $name;
if ($callback) {
$callback($filename);
}
}
}
}
closedir($handle);
}
return $files;
}
?> Login or register to post comments
Comments
$files array structure
I think its very useful to show the structure of the array that results from this function.
Array
(
[path/to/file.txt] => stdClass Object
(
[filename] => path/to/file.txt
[basename] => file.txt
[name] => file
)
)
The array is ordered by
The array is ordered by timestamp, not filename. So if you step through the results, the oldest file will be the first.
Sort order of files
@rschwab: Actually, since file_scan_directory() uses readdir(), the files will be returned in the order in which they are stored on the filesystem (according to the PHP documentation). So, it depends on your file system. The only way to guarantee the sort order of your files is to use some sort functions, like ksort or krsort.
$mask is case sensitive
Its worth noting that the $mask attribute is case sensitive.
$mask is ereg type regex
also note that $mask is old, ereg regular expression
in 7 it's pcre
Simple example
This example shows how to find all files of a certain type (in this case .inc) in a specific module.
$directory = drupal_get_path('module', 'mymodule') . '/subfolder';$mask = '.inc';
$files = file_scan_directory($directory, $mask);
I would like to report a bug
The function file_scan_directory has a little bug.
When you use it to scan a folder for images, for example, it returns a list of files with one extra forward slash as you can see on the following example:
<?php
$files = file_scan_directory('files', '.jpg');
foreach ($files as $value) {
echo $value->filename . '<br>';
}
?>
The above example will print the following:
sites/drupal/files//image1.jpg
sites/drupal/files//image2.jpg
sites/drupal/files//image3.jpg
sites/drupal/files//image4.jpg
etc...
Notice that the file names are preceded by two forward slashes.