(PHP 5, PHP 7, PHP
scandir — List files and directories inside the specified path
Description
scandir(string $directory
, int $sorting_order
= SCANDIR_SORT_ASCENDING
, ?resource $context
= null
): array|false
Parameters
-
directory
-
The directory that will be scanned.
-
sorting_order
-
By default, the sorted order is alphabetical in ascending order. If
the optionalsorting_order
is set to
SCANDIR_SORT_DESCENDING
, then the sort order is
alphabetical in descending order. If it is set to
SCANDIR_SORT_NONE
then the result is unsorted. -
context
-
For a description of the
context
parameter,
refer to the streams section of
the manual.
Return Values
Returns an array of filenames on success, or false
on
failure. If directory
is not a directory, then
boolean false
is returned, and an error of level
E_WARNING
is generated.
Changelog
Version | Description |
---|---|
8.0.0 |
context is now nullable.
|
Examples
Example #1 A simple scandir() example
<?php
$dir = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, SCANDIR_SORT_DESCENDING);print_r($files1);
print_r($files2);
?>
The above example will output
something similar to:
Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . )
Notes
Tip
A URL can be used as a
filename with this function if the fopen wrappers have been enabled.
See fopen() for more details on how to specify the
filename. See the Supported Protocols and Wrappers for links to information
about what abilities the various wrappers have, notes on their usage,
and information on any predefined variables they may
provide.
See Also
- opendir() — Open directory handle
- readdir() — Read entry from directory handle
- glob() — Find pathnames matching a pattern
- is_dir() — Tells whether the filename is a directory
- sort() — Sort an array in ascending order
dwieeb at gmail dot com ¶
11 years ago
Easy way to get rid of the dots that scandir() picks up in Linux environments:
<?php
$directory = '/path/to/my/directory';
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
?>
mmda dot nl at gmail dot com ¶
10 years ago
Here is my 2 cents. I wanted to create an array of my directory structure recursively. I wanted to easely access data in a certain directory using foreach. I came up with the following:
<?php
function dirToArray($dir) {
$result = array();
$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
}
else
{
$result[] = $value;
}
}
}
return
$result;
}
?>
Output
Array
(
[subdir1] => Array
(
[0] => file1.txt
[subsubdir] => Array
(
[0] => file2.txt
[1] => file3.txt
)
)
[subdir2] => Array
(
[0] => file4.txt
}
)
info at remark dot no ¶
5 years ago
Someone wrote that array_slice could be used to quickly remove directory entries "." and "..". However, "-" is a valid entry that would come before those, so array_slice would remove the wrong entries.
coolbikram0 at gmail dot com ¶
1 year ago
A simple recursive function to list all files and subdirectories in a directory:
<?php
function listAllFiles($dir) {
$array = array_diff(scandir($dir), array('.', '..'));
foreach (
$array as &$item) {
$item = $dir . $item;
}
unset($item);
foreach ($array as $item) {
if (is_dir($item)) {
$array = array_merge($array, listAllFiles($item . DIRECTORY_SEPARATOR));
}
}
return $array;
}
?>
Pawel Dlugosz ¶
18 years ago
For directory containing files like (for example) -.jpg the results of scandir are a little "weird" ;)
<?php
$dir
= '/somedir';
$files = scandir($dir);
print_r($files);
?>
Array
(
[0] => -.jpg
[1] => .
[2] => ..
[3] => foo.txt
[4] => somedir
)
Beware - sorting is in ASCII order :)
kodlee at kodleeshare dot net ¶
11 years ago
I needed to find a way to get the full path of all files in the directory and all subdirectories of a directory.
Here's my solution: Recursive functions!
<?php
function find_all_files($dir)
{
$root = scandir($dir);
foreach($root as $value)
{
if($value === '.' || $value === '..') {continue;}
if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;}
foreach(find_all_files("$dir/$value") as $value)
{
$result[]=$value;
}
}
return $result;
}
?>
eep2004 at ukr dot net ¶
8 years ago
Fastest way to get a list of files without dots.
<?php
$files = array_slice(scandir('/path/to/directory/'), 2);
gambit_642 AT hotmailDOTcom ¶
9 years ago
Needed something that could return the contents of single or multiple directories, recursively or non-recursively,
for all files or specified file extensions that would be
accessible easily from any scope or script.
And I wanted to allow overloading cause sometimes I'm too lazy to pass all params.
<?php
class scanDir {
static private $directories, $files, $ext_filter, $recursive;// ----------------------------------------------------------------------------------------------
// scan(dirpath::string|array, extensions::string|array, recursive::true|false)
static public function scan(){
// Initialize defaults
self::$recursive = false;
self::$directories = array();
self::$files = array();
self::$ext_filter = false;// Check we have minimum parameters
if(!$args = func_get_args()){
die("Must provide a path string or array of path strings");
}
if(gettype($args[0]) != "string" && gettype($args[0]) != "array"){
die("Must provide a path string or array of path strings");
}// Check if recursive scan | default action: no sub-directories
if(isset($args[2]) && $args[2] == true){self::$recursive = true;}// Was a filter on file extensions included? | default action: return all file types
if(isset($args[1])){
if(gettype($args[1]) == "array"){self::$ext_filter = array_map('strtolower', $args[1]);}
else
if(gettype($args[1]) == "string"){self::$ext_filter[] = strtolower($args[1]);}
}// Grab path(s)
self::verifyPaths($args[0]);
return self::$files;
}
static private function
verifyPaths($paths){
$path_errors = array();
if(gettype($paths) == "string"){$paths = array($paths);}
foreach(
$paths as $path){
if(is_dir($path)){
self::$directories[] = $path;
$dirContents = self::find_contents($path);
} else {
$path_errors[] = $path;
}
}
if(
$path_errors){echo "The following directories do not exists<br />";die(var_dump($path_errors));}
}// This is how we scan directories
static private function find_contents($dir){
$result = array();
$root = scandir($dir);
foreach($root as $value){
if($value === '.' || $value === '..') {continue;}
if(is_file($dir.DIRECTORY_SEPARATOR.$value)){
if(!self::$ext_filter || in_array(strtolower(pathinfo($dir.DIRECTORY_SEPARATOR.$value, PATHINFO_EXTENSION)), self::$ext_filter)){
self::$files[] = $result[] = $dir.DIRECTORY_SEPARATOR.$value;
}
continue;
}
if(self::$recursive){
foreach(self::find_contents($dir.DIRECTORY_SEPARATOR.$value) as $value) {
self::$files[] = $result[] = $value;
}
}
}
// Return required for recursive search
return $result;
}
}
?>
Usage:
scanDir::scan(path(s):string|array, [file_extensions:string|array], [subfolders?:true|false]);
<?php
//Scan a single directory for all files, no sub-directories
$files = scanDir::scan('D:Websitestemp');//Scan multiple directories for all files, no sub-dirs
$dirs = array(
'D:folder';
'D:folder2';
'C:Other';
);
$files = scanDir::scan($dirs);// Scan multiple directories for files with provided file extension,
// no sub-dirs
$files = scanDir::scan($dirs, "jpg");
//or with an array of extensions
$file_ext = array(
"jpg",
"bmp",
"png"
);
$files = scanDir::scan($dirs, $file_ext);// Scan multiple directories for files with any extension,
// include files in recursive sub-folders
$files = scanDir::scan($dirs, false, true);// Multiple dirs, with specified extensions, include sub-dir files
$files = scanDir::scan($dirs, $file_ext, true);
?>
csaba at alum dot mit dot edu ¶
18 years ago
Scandir on steroids:
For when you want to filter your file list, or only want to list so many levels of subdirectories...
<?php
function dirList($path="", $types=2, $levels=1, $aFilter=array()) {
// returns an array of the specified files/directories
// start search in $path (defaults to current working directory)
// return $types: 2 => files; 1 => directories; 3 => both;
// $levels: 1 => look in the $path only; 2 => $path and all children;
// 3 => $path, children, grandchildren; 0 => $path and all subdirectories;
// less than 0 => complement of -$levels, OR everything starting -$levels down
// e.g. -1 => everthing except $path; -2 => all descendants except $path + children
// Remaining argument(s) is(are) a filter array(list) of regular expressions which operate on the full path.
// First character (before the '/' of the regExp) '-' => NOT.
// First character (after a possible '-') 'd' => apply to directory name
// The filters may be passed in as an array of strings or as a list of strings
// Note that output directories are prefixed with a '*' (done in the line above the return)
$dS = DIRECTORY_SEPARATOR;
if (!($path = realpath($path?$path:getcwd()))) return array(); // bad path
// next line rids terminating on drives (works since c: == c: on PHP). OK in *nix?
if (substr($path,-1)==$dS) $path = substr($path,0,-1);
if (is_null($types)) $types = 2;
if (is_null($levels)) $levels = 1;
if (is_null($aFilter)) $aFilter=array();
// last argument may be passed as a list or as an array
$aFilter = array_slice(func_get_args(),3);
if ($aFilter && gettype($aFilter[0])=="array") $aFilter=$aFilter[0];
$adFilter = array();
// now move directory filters to separate array:
foreach ($aFilter as $i=>$filter) // for each directory filter...
if (($pos=stripos(" $filter","d")) && $pos<3) { // next line eliminates the 'd'
$adFilter[] = substr($filter,0,$pos-1) . substr($filter,$pos);
unset($aFilter[$i]); }
$aFilter = array_merge($aFilter); // reset indeces
$aRes = array(); // results, $aAcc is an Accumulator
$aDir = array($path); // dirs to check
for ($i=$levels>0?$levels++:-1;($aAcc=array())||$i--&&$aDir;$aDir=$aAcc)
while ($dir = array_shift($aDir))
foreach (scandir ($dir) as $fileOrDir)
if ($fileOrDir!="." && $fileOrDir!="..") {
if ($dirP = is_dir ($rp="$dir$dS$fileOrDir"))
if (pathFilter("$rp$dS", $adFilter))
$aAcc[] = $rp;
if ($i<$levels-1 && ($types & (2-$dirP)))
if (pathFilter($rp, $aFilter))
$aRes[] = ($dirP?"*":"") . $rp; }
return $aRes;
}
?>
example usage:
<?php
define ("_", NULL);
// this will find all non .jpg, non .Thumbs.db files under c:Photo
$aFiles = dirList('c:Photo', _, 0, '-/.jpg$/i', '-/\\Thumbs.db$/');
$aFiles = dirList(); // find the files in the current directory
// next lines will find .jpg files in non Photo(s) subdirectories, excluding Temporary Internet Files
set_time_limit(60); // iterating from the top level can take a while
$aFiles = dirList("c:\", _, 0, '/.jpg$/i', '-d/\\Photos?$/i', '-d/Temporary Internet/i');
?>
Note that this function will consume a lot of time if scanning large
directory structures (which is the reason for the '[-]d/.../' filters).
Csaba Gabor from Vienna
artmanniako at gmail dot com ¶
4 years ago
How i solved problem with '.' and '..'
$x = scandir__DIR__; //any directory
foreach ($x as $key => $value) {
if ('.' !== $value && '..' !== $value){
echo $value;
}
}
Simple and working
Stan P. van de Burgt ¶
18 years ago
scandir() with regexp matching on file name and sorting options based on stat().
<?php
function myscandir($dir, $exp, $how='name', $desc=0)
{
$r = array();
$dh = @opendir($dir);
if ($dh) {
while (($fname = readdir($dh)) !== false) {
if (preg_match($exp, $fname)) {
$stat = stat("$dir/$fname");
$r[$fname] = ($how == 'name')? $fname: $stat[$how];
}
}
closedir($dh);
if ($desc) {
arsort($r);
}
else {
asort($r);
}
}
return(array_keys($r));
}
$r = myscandir('./book/', '/^article[0-9]{4}.txt$/i', 'ctime', 1);
print_r($r);
?>
files can be sorted on name and stat() attributes, ascending and descending:
name file name
dev device number
ino inode number
mode inode protection mode
nlink number of links
uid userid of owner
gid groupid of owner
rdev device type, if inode device *
size size in bytes
atime time of last access (Unix timestamp)
mtime time of last modification (Unix timestamp)
ctime time of last inode change (Unix timestamp)
blksize blocksize of filesystem IO *
blocks number of blocks allocated
fazle dot elahee at gmail dot com ¶
11 years ago
/**
* This function will scan all files recursively in the sub-folder and folder.
*
* @author Fazle Elahee
*
*/
function scanFileNameRecursivly($path = '', &$name = array() )
{
$path = $path == ''? dirname(__FILE__) : $path;
$lists = @scandir($path);
if(!empty($lists))
{
foreach($lists as $f)
{
if(is_dir($path.DIRECTORY_SEPARATOR.$f) && $f != ".." && $f != ".")
{
scanFileNameRecursivly($path.DIRECTORY_SEPARATOR.$f, &$name);
}
else
{
$name[] = $path.DIRECTORY_SEPARATOR.$f;
}
}
}
return $name;
}
$path = "/var/www/SimplejQueryDropdowns";
$file_names = scanFileNameRecursivly($path);
echo "<pre>";
var_dump($file_names);
echo "</pre>";
phpdotnet at lavavortex dot com ¶
14 years ago
How a ninja may retrieve a list of files, files filtered by extension, or directories:
<?php
//NNNIIIinnnjaaa::
//array of files without directories... optionally filtered by extension
function file_list($d,$x){
foreach(array_diff(scandir($d),array('.','..')) as $f)if(is_file($d.'/'.$f)&&(($x)?ereg($x.'$',$f):1))$l[]=$f;
return $l;
}
//NNNIIIinnnjaaa::
//array of directories
function dir_list($d){
foreach(array_diff(scandir($d),array('.','..')) as $f)if(is_dir($d.'/'.$f))$l[]=$f;
return $l;
}
/********************************************************************
PRETTY PRETTY LIGHTS (DOCUMENTATION)
********************************************************************/
/********************************************************************
Overloaded PHP file listing function:
array file_list ( string $directory [, string $file_extension] )
$directory
path without backslash, e.g. "/home/public_html/blarg"
$file_extention
optionally filter specific filename extentions, e.g. ".jpg"
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TRANSLATION <<<<<<<<<<<<<<<<<<<<<<<<<<<<
file_list("/home"); //return list of files (no directories)
file_list("/home", ".jpg"); //return only .jpg file list
********************************************************************/
//(note: one slash (/) below and you enable all your test functions, guess where ;-))
/********************************************************************
// TEST FUNCTIONS... IF THESE WORK, THIS FUNCTION WORKS ON THIS PLATFORM
echo "<hr><b>File List:</b><br>";
$n = file_list(getcwd());
if($n) foreach($n as $f) echo "$f<br>"; //current files
echo "<hr><b>Files with extension .php:</b><br>";
$n = file_list(getcwd(),".php");
if($n) foreach($n as $f) echo "$f<br>"; //files with .php extensions
echo "<hr><b>Directories:</b><br>";
$d = dir_list(getcwd());
if($d) foreach($d as $f) echo "$f<br>"; //directories
/********************************************************************/
/************
RUNTIME NOTES:
file_list($arg1); // php issues a warning that there is no second parameter, but we know that, izz ok
************/
/*******************************
TESTED AND WORKING ON 2009.04.30:
OS: Linux 2.6.9-78.0.17.ELsmp
APACHE: 2.2.9
PHP: 5.2.5
*******************************/
?>
arthur dot am dot app at gmail dot com ¶
2 months ago
$path_abs = __DIR__ .'/test/';
if( DIRECTORY_SEPARATOR != substr($path, -1) || (DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR) == substr($path, -2))
{
throw new Error("A non-standard path is specified. `{$path}`");
}
$scandir_result = scandir( $path, SCANDIR_SORT_DESCENDING );
for( $n = 0, $e = count($scandir_result) - 2; $n < $e; $n++ )
{
echo ($path . $scandir_result[$n]) .'<br/>';
}
fatpratmatt dot at dot gmail dot com ¶
15 years ago
This function generates a list of all files in the chosen directory and all subdirectories, throws them into a NON-multidimentional array and returns them.
Most of the recursive functions on this page only return a multi-dimensional array.
This is actually a modification of some one else's function (thanks mail at bartrail dot de ;])
<?php
function scanDirectories($rootDir, $allData=array()) {
// set filenames invisible if you want
$invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");
// run through content of root directory
$dirContent = scandir($rootDir);
foreach($dirContent as $key => $content) {
// filter all files not accessible
$path = $rootDir.'/'.$content;
if(!in_array($content, $invisibleFileNames)) {
// if content is file & readable, add to array
if(is_file($path) && is_readable($path)) {
// save file name with path
$allData[] = $path;
// if content is a directory and readable, add path and name
}elseif(is_dir($path) && is_readable($path)) {
// recursive callback to open new directory
$allData = scanDirectories($path, $allData);
}
}
}
return $allData;
}
?>
Example output:
print_r(scanDirectories("www"));
---
Array
(
[0] => www/index.php
[1] => www/admin.php
[3] => www/css/css.css
[4] => www/articles/2007/article1.txt
[4] => www/articles/2006/article1.txt
[8] => www/img/img1.png
)
Nerbert ¶
3 years ago
If you use array_diff() to eliminate "." and ".." you must use array_values() on the result because array_diff() will return an associative array, which may cause problems for a for loop beginning at 0.
$files = array_values(array_diff(scandir($directory), array('..', '.')));
SPekary ¶
5 years ago
Unless you specify no sorting, file names are sorted in ASCII alphabetic order, meaning numbers first, then uppercase, then lowercase letters, even on operating systems whose file system ignores the case of file names when it does its own sorting.
For example, on Mac OS, the following files will appear in this order in the Finder, when your disk is formated using the standard file system:
1file.php
a.inc
B.txt
c.txt
However, scandir will produce an array in the following order:
1file.php
B.txt
a.inc
c.txt
carneiro at isharelife dot com dot br ¶
10 years ago
<?php
/**
* Get an array that represents directory tree
* @param string $directory Directory path
* @param bool $recursive Include sub directories
* @param bool $listDirs Include directories on listing
* @param bool $listFiles Include files on listing
* @param regex $exclude Exclude paths that matches this regex
*/
function directoryToArray($directory, $recursive = true, $listDirs = false, $listFiles = true, $exclude = '') {
$arrayItems = array();
$skipByExclude = false;
$handle = opendir($directory);
if ($handle) {
while (false !== ($file = readdir($handle))) {
preg_match("/(^(([.]){1,2})$|(.(svn|git|md))|(Thumbs.db|.DS_STORE))$/iu", $file, $skip);
if($exclude){
preg_match($exclude, $file, $skipByExclude);
}
if (!$skip && !$skipByExclude) {
if (is_dir($directory. DIRECTORY_SEPARATOR . $file)) {
if($recursive) {
$arrayItems = array_merge($arrayItems, directoryToArray($directory. DIRECTORY_SEPARATOR . $file, $recursive, $listDirs, $listFiles, $exclude));
}
if($listDirs){
$file = $directory . DIRECTORY_SEPARATOR . $file;
$arrayItems[] = $file;
}
} else {
if($listFiles){
$file = $directory . DIRECTORY_SEPARATOR . $file;
$arrayItems[] = $file;
}
}
}
}
closedir($handle);
}
return $arrayItems;
}
?>
simon dot riget at gmail dot com ¶
6 years ago
This is a simple and versatile function that returns an array tree of files, matching wildcards:
<?php
// List files in tree, matching wildcards * and ?
function tree($path){
static $match;// Find the real directory part of the path, and set the match parameter
$last=strrpos($path,"/");
if(!is_dir($path)){
$match=substr($path,$last);
while(!is_dir($path=substr($path,0,$last)) && $last!==false)
$last=strrpos($path,"/",-1);
}
if(empty($match)) $match="/*";
if(!$path=realpath($path)) return;// List files
foreach(glob($path.$match) as $file){
$list[]=substr($file,strrpos($file,"/")+1);
} // Process sub directories
foreach(glob("$path/*", GLOB_ONLYDIR) as $dir){
$list[substr($dir,strrpos($dir,"/",-1)+1)]=tree($dir);
}
return @
$list;
}
?>
telefoontoestel59 at hotmail dot com ¶
7 years ago
I was looking for an easy way to get only files from a certain directory. I came up with the following line of code that will result in an array listing only files. the samen can be done for directories ofcourse.
<?php
$files = array_filter(scandir($directory), function($file) { return is_file($file); })
?>
Tom ¶
8 years ago
Just was curious to count files and lines in a project
<?phpfunction DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
{$file_read = array( 'php', 'html', 'js', 'css' );
$dir_ignore = array();$scan_result = scandir( $dir );
foreach (
$scan_result as $key => $value ) {
if ( !
in_array( $value, array( '.', '..' ) ) ) {
if (
is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) {
if (
in_array( $value, $dir_ignore ) ) {
continue;
}$result = DirLineCounter( $dir . DIRECTORY_SEPARATOR . $value, $result, false );
}
else {
$type = explode( '.', $value );
$type = array_reverse( $type );
if( !in_array( $type[0], $file_read ) ) {
continue;
}$lines = 0;
$handle = fopen( $dir . DIRECTORY_SEPARATOR . $value, 'r' );
while ( !
feof( $handle ) ) {
if (
is_bool( $handle ) ) {
break;
}$line = fgets( $handle );
$lines++;
}fclose( $handle );$result['lines_html'][] = '<tr><td>' . $dir . '</td><td>' . $value . '</td><td>' . $lines . '</td></tr>';
$result['lines_count'] = $result['lines_count'] + $lines;
$result['files_count'] = $result['files_count'] + 1;
}
}
}
if (
$complete_table ) {$lines_html = implode('', $result['lines_html']) . '<tr><td></td><td style="border: 1px solid #222">Files Total: ' . $result['files_count'] . '</td><td style="border: 1px solid #222">Lines Total: ' . $result['lines_count'] . '</td></tr>';
return '<table><tr><td style="width: 60%; background-color:#ddd;">Dir</td><td style="width: 30%; background-color:#ddd;">File</td><td style="width: 10%; background-color:#ddd;">Lines</td></tr>' . $lines_html . '</table>';
}
else {
return
$result;
}
}
echo
DirLineCounter( '.' );?>
Patrick ¶
9 years ago
Here is my recursive function, placing directories into new array keys and its contents within.
/* find_files( string, &array )
** Recursive function to return a multidimensional array of folders and files
** that are contained within the directory given
*/
function find_files($dir, &$dir_array)
{
// Create array of current directory
$files = scandir($dir);
if(is_array($files))
{
foreach($files as $val)
{
// Skip home and previous listings
if($val == '.' || $val == '..')
continue;
// If directory then dive deeper, else add file to directory key
if(is_dir($dir.'/'.$val))
{
// Add value to current array, dir or file
$dir_array[$dir][] = $val;
find_files($dir.'/'.$val, $dir_array);
}
else
{
$dir_array[$dir][] = $val;
}
}
}
ksort($dir_array);
}
// Example
$folder_list = array();
find_files('/path', $folder_list)
var_dump($folder_list);
array(3) {
["directory_01"]=>
array(4) {
[0]=>
string(12) "directory_02"
[1]=>
string(12) "directory_03"
[2]=>
string(11) "file_01.txt"
[3]=>
string(11) "file_02.txt"
}
["directory_01/directory_02"]=>
array(2) {
[0]=>
string(11) "file_03.txt"
[1]=>
string(11) "file_04.txt"
}
["directory_01/directory_03"]=>
array(2) {
[0]=>
string(11) "file_05.txt"
[1]=>
string(11) "file_06.txt"
}
}
2bbasic at gmail dot com ¶
10 years ago
<?php
//-- Directory Navigation with SCANDIR
//--
//-- optional placemenet
$exclude_list = array(".", "..", "example.txt");
if (isset($_GET["dir"])) {
$dir_path = $_SERVER["DOCUMENT_ROOT"]."/".$_GET["dir"];
}
else {
$dir_path = $_SERVER["DOCUMENT_ROOT"]."/";
}
//-- until here
function dir_nav() {
global $exclude_list, $dir_path;
$directories = array_diff(scandir($dir_path), $exclude_list);
echo "<ul style='list-style:none;padding:0'>";
foreach($directories as $entry) {
if(is_dir($dir_path.$entry)) {
echo "<li style='margin-left:1em;'>[`] <a href='?dir=".$_GET["dir"].$entry."/"."'>".$entry."</a></li>";
}
}
echo "</ul>";
//-- separator
echo "<ul style='list-style:none;padding:0'>";
foreach($directories as $entry) {
if(is_file($dir_path.$entry)) {
echo "<li style='margin-left:1em;'>[ ] <a href='?file=".$_GET["dir"].$entry."'>".$entry."</a></li>";
}
}
echo "</ul>";
}
dir_nav();
//-- optional placement
if (isset($_GET["file"])) {
echo "<div style='margin:1em;border:1px solid Silver;'>";
highlight_file($dir_path.$_GET['file']);
echo "</div>";
}
//-- until here
//--
//-- Because I love php.net
?>
moik78 at gmail dot com ¶
13 years ago
This is a function to count the number of files of a directory
<?php
function countFiles($dir){
$files = array();
$directory = opendir($dir);
while($item = readdir($directory)){
// We filter the elements that we don't want to appear ".", ".." and ".svn"
if(($item != ".") && ($item != "..") && ($item != ".svn") ){
$files[] = $item;
}
}
$numFiles = count($files);
return $numFiles;
}
?>
magicmind at netcabo dot pt ¶
13 years ago
Hello all,
I just added a extension filter to the getDirectoryTree function, so it can filter an extension for files in the folders/subfolders:
<?php
function getDirectoryTree( $outerDir , $x){
$dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) );
$dir_array = Array();
foreach( $dirs as $d ){
if( is_dir($outerDir."/".$d) ){
$dir_array[ $d ] = getDirectoryTree( $outerDir."/".$d , $x);
}else{
if (($x)?ereg($x.'$',$d):1)
$dir_array[ $d ] = $d;
}
}
return $dir_array;
}
$dirlist = getDirectoryTree('filmes','flv');
?>
progrium+php at gmail dot com ¶
17 years ago
I made this to represent a directory tree in an array that uses the file or directory names as keys and full paths as the value for files. Directories are nested arrays.
<?php
function generatePathTree($rootPath) {
$pathStack = array($rootPath);
$contentsRoot = array();
$contents = &$contentsRoot;
while ($path = array_pop($pathStack)) {
$contents[basename($path)] = array();
$contents = &$contents[basename($path)];
foreach (scandir($path) as $filename) {
if ('.' != substr($filename, 0, 1)) {
$newPath = $path.'/'.$filename;
if (is_dir($newPath)) {
array_push($pathStack, $newPath);
$contents[basename($newPath)] = array();
} else {
$contents[basename($filename)] = $newPath;
}
}
}
}
return $contentsRoot[basename($rootPath)];
}
?>
The function will return something like this:
Array
(
[index.php] => /usr/local/www/index.php
[js] => Array
(
[async.js] => /usr/local/www/js/async.js
[dom.js] => /usr/local/www/js/dom.js
[effects.js] => /usr/local/www/js/effects.js
[prototype.js] => /usr/local/www/js/prototype.js
)
[logo.png] => /usr/local/www/logo.png
[server.php] => /usr/local/www/server.php
[test.js] => /usr/local/www/test.js
)
dsiembab at fullchannel dot net ¶
14 years ago
Back in the saddle of scandir I wrote this function for a function that I needed to seperate directories from files. Since I am still learning from my last example way below I would figure I would add it so it can be criticized.
<?php
function dirlist($dir, $bool = "dirs"){
$truedir = $dir;
$dir = scandir($dir);
if($bool == "files"){ // dynamic function based on second pram
$direct = 'is_dir';
}elseif($bool == "dirs"){
$direct = 'is_file';
}
foreach($dir as $k => $v){
if(($direct($truedir.$dir[$k])) || $dir[$k] == '.' || $dir[$k] == '..' ){
unset($dir[$k]);
}
}
$dir = array_values($dir);
return $dir;
}
?>
<?php
print_r(dirlist("../")); //confirm array of subdirectoriesprint_r(dirlist("../", "files") // confirm list on files in the directory
?>
beingmrkenny at gmail dot com ¶
16 years ago
I wrote this function to read a folder and place all the folders and sub folders it contains into an array.
<?php// Initialise empty array, otherwise an error occurs
$folders = array();
function
recursive_subfolders($folders) {// Set path here
$path = '/path/to/folder';// Create initial "Folders" array
if ($dir = opendir($path)) {
$j = 0;
while (($file = readdir($dir)) !== false) {
if ($file != '.' && $file != '..' && is_dir($path.$file)) {
$j++;
$folders[$j] = $path . $file;
}
}
}closedir($dir);// Then check each folder in that array for subfolders and add the subfolders to the "Folders" array.
$j = count($folders);
foreach ($folders as $folder) {
if ($dir = opendir($folder)) {
while (($file = readdir($dir)) !== false) {
$pathto = $folder. '/' . $file;
if ($file != '.' && $file != '..' && is_dir($pathto) && !in_array($pathto, $folders)) {
$j++;
$folders[$j] = $pathto;
$folders = recursive_subfolders($folders);
}
}
}
closedir($dir);
}sort($folders);
return $folders;
}$folders = recursive_subfolders($folders);?>
$folders now contains an array with the full paths to each subfolder. E.g.:
Array
(
[0] => /path/to/folder/dir1
[1] => /path/to/folder/dir1/subdir
[2] => /path/to/folder/dir1/subdir/subsubdir
[3] => /path/to/dolfer/dir2
)
This function has only been tested on Linux.
webmaster at gmail dot com ¶
3 years ago
easy way to list every item in a directory
<?php
$itemnum = 1;
$dir = 'C:/Path/To/Directory';
foreach(scandir($dir) as $item){
if (!($item == '.')) {
if (!($item == '..')) {
echo($itemnum." = ".$item."<br>");
$itemnum = ($itemnum + 1);
}}}
?>
phpnet at novaclic dot com ¶
13 years ago
Was looking for a simple way to search for a file/directory using a mask. Here is such a function.
By default, this function will keep in memory the scandir() result, to avoid scaning multiple time for the same directory.
Requires at least PHP5.
<?php
function sdir( $path='.', $mask='*', $nocache=0 ){
static $dir = array(); // cache result in memory
if ( !isset($dir[$path]) || $nocache) {
$dir[$path] = scandir($path);
}
foreach ($dir[$path] as $i=>$entry) {
if ($entry!='.' && $entry!='..' && fnmatch($mask, $entry) ) {
$sdir[] = $entry;
}
}
return ($sdir);
}
?>
boen dot robot at gmail dot com ¶
13 years ago
If you have a folder with many files and/or subfolders, doing a recursive scandir will likely either slow down your application, or cause a high rise in RAM consumption due to the large size of the generated array.
To help with this, as well as to make processing of files in a folder easier, I wrote a function that reads a folder and its subfolders recursively, and calls a function upon each match.
<?php
/**
* Calls a function for every file in a folder.
*
* @author Vasil Rangelov a.k.a. boen_robot
*
* @param string $callback The function to call. It must accept one argument that is a relative filepath of the file.
* @param string $dir The directory to traverse.
* @param array $types The file types to call the function for. Leave as NULL to match all types.
* @param bool $recursive Whether to list subfolders as well.
* @param string $baseDir String to append at the beginning of every filepath that the callback will receive.
*/
function dir_walk($callback, $dir, $types = null, $recursive = false, $baseDir = '') {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
if (is_file($dir . $file)) {
if (is_array($types)) {
if (!in_array(strtolower(pathinfo($dir . $file, PATHINFO_EXTENSION)), $types, true)) {
continue;
}
}
$callback($baseDir . $file);
}elseif($recursive && is_dir($dir . $file)) {
dir_walk($callback, $dir . $file . DIRECTORY_SEPARATOR, $types, $recursive, $baseDir . $file . DIRECTORY_SEPARATOR);
}
}
closedir($dh);
}
}
?>
Of course, because it is recursive, a folder with many levels of folders could potentially consume lots of memory, but then again, so can every other recursive scandir implementation here.
BTW, there's also the RecursiveDirectoryIterator SPL class:
http://bg.php.net/manual/en/class.recursivedirectoryiterator.php
which, even if using the same approach, will most likely be faster and hold down deeper levels (because it works on the C level), but this one will always work, regardless of settings... even on PHP4.
Point is, avoid recursive scandir implementations.
cHH ¶
15 years ago
Since scandir() returns and array, here is a more concise method of dealing with the '.' and '..' problem when listing directories:
<?php
$target = '/';
$weeds = array('.', '..');
$directories = array_diff(scandir($target), $weeds);
foreach(
$directories as $value)
{
if(is_dir($target.$value))
{
echo $value.'<br />';
}
}
?>
asamir at asamir dot net ¶
15 years ago
This is a modification of scanDirectories function that generates a list of all files in the chosen directory and all subdirectories of specific extentions $allowext
<?php
function scanDirectories($rootDir, $allowext, $allData=array()) {
$dirContent = scandir($rootDir);
foreach($dirContent as $key => $content) {
$path = $rootDir.'/'.$content;
$ext = substr($content, strrpos($content, '.') + 1);
if(
in_array($ext, $allowext)) {
if(is_file($path) && is_readable($path)) {
$allData[] = $path;
}elseif(is_dir($path) && is_readable($path)) {
// recursive callback to open new directory
$allData = scanDirectories($path, $allData);
}
}
}
return $allData;
}$rootDir = "www";
$allowext = array("zip","rar","html");
$files_array = scanDirectories($rootDir,$allowext);
print_r($files_array);
?>
csaba at alum dot mit dot edu ¶
18 years ago
A nice way to filter the files/directories you get back from scandir:
<?php
function pathFilter ($path, $aFilter) {
// returns true iff $path survives the tests from $aFilter
// $aFilter is an array of regular expressions: [-]/regExp/modifiers
// if there is a leading '-' it means exclude $path upon a match (a NOT test)
// If the first expression has a leading '-', $path is in, unless it gets excluded.
// Otherwise, $path is out, unless it gets included via the filter tests.
// The tests are applied in succession.
// A NOT test is applied only if $path is currently (within the tests) included
// Other tests are applied only if $path is currently excluded. Thus,
// array("/a/", "-/b/", "/c/") => passes if $path has a c or if $path has an a but no b
// array("/a/", "/c/", "-/b/") => passes if $path has an a or c, but no b
// array("-/b/", "/a/", "/c/") => passes if $path has no b, or if $path has an a or c
if (!$aFilter) return true; // automatic inclusion (pass) if no filters
foreach ($aFilter as $filter) break; // we don't know how it's indexed
$in = $filter[0]=="-"; // initial in/exclusion based on first filter
foreach ($aFilter as $filter) // walk the filters
if ($in==$not=($filter[0]=="-")) // testing only when necessary
$in ^= preg_match(substr($filter,$not),$path); // flip in/exclusion upon a match
return $in;
}
?>
Csaba Gabor from Vienna
admin at kmoz000 dot free ¶
4 years ago
function ScaniDir($path){
$ptd= opendir($path);
while (( $ptdf=readdir($ptd))){
$srca = $path.'/'.$ptdf;
if (is_file($srca) and pathinfo($srca, PATHINFO_EXTENSION)=='pdf') {$files['src'][]=$srca;$files['name'][]=explode('.',$ptdf)[0];}
}
return $files ;
}
hex at mail dot nnov dot ru ¶
9 years ago
// Extreme minimal version recursive scan:
function rscandir($dir){
$dirs = array_fill_keys( array_diff( scandir( $dir ), array( '.', '..' ) ), array());
foreach( $dirs as $d => $v )
if( is_dir($dir."/".$d) )
$dirs[$d] = rscandir($dir."/".$d);
return $dirs;
}
djacobson at usa dot rugby ¶
3 years ago
<?php
/**
* Get rid of the dots, the occasional `.DS_Store` file and reindex the
* resulting array all at once.
*
* @param string $path_to_directory The relative path to your target directory.
*
* @return array The reindexed array of files.
*/
function get_the_files( $path_to_directory ) {
return array_slice( array_diff( scandir( $path_to_directory ), array( '..', '.', '.DS_Store' ) ), 0 );
}
?>
wwwebdesigner at web doott de ¶
18 years ago
using sort of scandir() that returns the content sorted by Filemodificationtime.
<?php
function scandir_by_mtime($folder) {
$dircontent = scandir($folder);
$arr = array();
foreach($dircontent as $filename) {
if ($filename != '.' && $filename != '..') {
if (filemtime($folder.$filename) === false) return false;
$dat = date("YmdHis", filemtime($folder.$filename));
$arr[$dat] = $filename;
}
}
if (!ksort($arr)) return false;
return $arr;
}
?>
returns false if an error occured
otherwise it returns an array like this.
Array
(
[20040813231320] => DSC00023.JPG
[20040813231456] => DSC00024.JPG
[20040814003728] => MOV00055.MPG
[20040814003804] => DSC00056.JPG
[20040814003946] => DSC00057.JPG
[20040814004030] => DSC00058.JPG
[20040814014516] => DSC00083.JPG
[20050401161718] => album.txt
)
За последние 24 часа нас посетили 11106 программистов и 943 робота. Сейчас ищут 669 программистов …
scandir
(PHP 5, PHP 7)
scandir — Получает список файлов и каталогов, расположенных по указанному пути
Описание
array scandir
( string $directory
[, int $sorting_order
= SCANDIR_SORT_ASCENDING
[, resource $context
]] )
Список параметров
-
directory
-
Сканируемый каталог.
-
sorting_order
-
По умолчанию, сортировка производится в алфавитном порядке
по возрастанию. Если необязательный параметр
sorting_order
установлен в значение
SCANDIR_SORT_DESCENDING
, сортировка производится
в алфавитном порядке по убыванию. Если же он установлен в значение
SCANDIR_SORT_NONE
, то сортировка не производится. -
context
-
За описанием параметра
context
обратитесь к разделу «Потоки»
данного руководства.
Возвращаемые значения
Возвращает array имен файлов в случае успеха или
FALSE
в случае ошибки. Если directory
не
является каталогом, возвращается FALSE
и генерируется сообщение
об ошибке уровня E_WARNING
.
Список изменений
Версия | Описание |
---|---|
5.4.0 |
Были добавлены sorting_order константы.Любое ненулевое значение задавало сортировку по убыванию в предыдущих версиях. Поэтому для всех версий PHP нужно использовать 0 для сортировки по возрастанию и 1 для сортировки по убыванию. Опции для режима SCANDIR_SORT_NONE не существовалодо PHP 5.4.0. |
Примеры
Пример #1 Простой пример использования функции scandir()
<?php
$dir = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);print_r($files1);
print_r($files2);
?>
Результатом выполнения данного примера
будет что-то подобное:
Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . )
Пример #2 Альтернативный вариант функции scandir() для PHP 4
<?php
$dir = "/tmp";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}sort($files);print_r($files);rsort($files);print_r($files);?>
Результатом выполнения данного примера
будет что-то подобное:
Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . )
Примечания
Подсказка
Для этой функции вы можете использовать URL
в качестве имени файла, если была включена опция fopen wrappers. Смотрите более
подробную информацию об определении имени файла в описании функции
fopen(). Смотрите также список поддерживаемых оберток URL, их возможности, замечания по использованию и список
предопределенных констант в Поддерживаемые протоколы и обработчики (wrappers).
Смотрите также
- opendir() — Открывает дескриптор каталога
- readdir() — Получает элемент каталога по его дескриптору
- glob() — Находит файловые пути, совпадающие с шаблоном
- is_dir() — Определяет, является ли имя файла директорией
- sort() — Сортирует массив
Вернуться к: Функции для работы с каталогами
В данном уроке мы разберемся с типовой задачей, которая возникает во время работы над проектом PHP: получение списка файлов и директорий. Обсудим несколько базовых и более изощренных подходов, с перечисление плюсов и минусов каждого. Первые три решения будут использовать типовые функции PHP, а затем представим более надежное с применением итераторов SPL.
Для предметного обсуждения решения и демонстраций предположим, что структура директорий имеет следующий вид:
---manager | ---user | ---document.txt | ---data.dat | ---style.css |---article.txt |---master.dat |---script.php |---test.dat |---text.txt
Базовые решения
Первый набор решений основан на использовании функции glob()
, комбинации функций opendir()
, readdir()
и closedir()
, и также функции scandir()
.
Использование glob()
Первое решение базируется на использовании функции glob(),
которая позволяет выполнять поиск пути с помощью шаблонов. Функция имеет два параметра:
$pattern
(обязательный): шаблон поиска$flags
(опциональный): один или несколько флагов, описание которых можно найти в документации
Рассмотрим примеры. Для поиска в директории всех файлов и директорий, имена которых заканчиваются на .txt, следует использовать код:
<?php $filelist = glob("*.txt");
Если вывести переменную $filelist
, то получим:
array ( 0 => 'article.txt', 1 => 'text.txt' )
Если нужен список файлов и директорий, имена которых начинаются на “te”, то код будет выглядеть так:
<?php $filelist = glob("te*");
А вывод имеет вид:
array ( 0 => 'test.dat', 1 => 'text.txt' )
А для получения списка только директорий с именами, содержащих “ma”, используем код:
<?php $filelist = glob("*ma*", GLOB_ONLYDIR);
Последний пример выведет:
Обратите внимание, что в последнем примере использован флаг GLOB_ONLYDIR
в качестве второго параметра функции. Поэтому файл master.dat
исключен из списка. Хотя функция glob()
очень проста в использовании, иногда она недостаточно гибкая. Например, нет флага для получения только файлов (без директорий), которые соответствуют шаблону.
Используем opendir()
, readdir()
, и closedir()
.
Второй подход к получению списка файлов и директорий, который мы обсудим, заключается в использовании функций opendir()
, readdir()
, и closedir()
.
Функция opendir()
открывает директорию и возвращает дескриптор соединения. Как только дескриптор получен, можно использовать функцию readdir()
. С каждым обращением данная функция выдает имя следующего файла или директории внутри открытого каталога. Если все имена уже были перечислены, функция возвращает false. Для закрытия дескриптора используется функция closedir()
.
В отличие от использования функции glob()
, данный подход сложнее, так как у вас нет параметров, которые помогают фильтровать список возвращаемых имен файлов и директорий. Вы должны выполнить фильтрацию самостоятельно, чтобы получить нужный результат.
Ниже приведенный пример возвращает список имен файлов и директорий начинающихся на “te”:
<?php $filelist = array(); if ($handle = opendir(".")) { while ($entry = readdir($handle)) { if (strpos($entry, "te") === 0) { $filelist[] = $entry; } } closedir($handle); }
При выполнении выше приведенного кода, переменная $entry
будет содержать такие включения, как “.” и “..”. Это две виртуальные директории, которые имеются в каждом каталоге файловой системы. Они представляют текущий каталог и родительский каталог соответственно.
Второй пример выводит только файлы, содержащиеся в заданном каталоге.
<?php $filelist = array(); if ($handle = opendir(".")) { while ($entry = readdir($handle)) { if (is_file($entry)) { $filelist[] = $entry; } } closedir($handle); }
Пример выдаст следующее:
array ( 0 => 'article.txt', 1 => 'master.dat', 2 => 'script.php', 3 => 'test.dat', 4 => 'text.txt' )
Использование scandir()
В завершение представим функцию scandir()
. Она имеет только один обязательный параметр: путь для чтения. Функция возвращает массив файлов и директорий, расположенных по указанному пути. Для получения списка файлов и директорий по определенному критерию нужно выполнить дополнительную фильтрацию. С другой стороны, решение получается более кратким и не требует управления дескрипторами.
Данный пример показывает, как получить список файлов и каталогов, имена которых начинаются на “te”:
<?php $entries = scandir("."); $filelist = array(); foreach($entries as $entry) { if (strpos($entry, "te") === 0) { $filelist[] = $entry; } }
Воспользуемся итераторами SPL
Теперь рассмотрим использование итераторов SPL. Но прежде, чем приступить к решению нашей задачи, проведем введение в библиотеку SPL и итераторы. Библиотека SPL предоставляет серию классов для объектно ориентированных структур данных, итераторов, дескрипторов файлов и прочее.
Одно из преимуществ итераторов заключается в том, что они являются классами и их можно расширить для удовлетворения собственных нужд. Другой плюс состоит в том, что итераторы имеют собственные методы, которые являются полезными при решении множества типовых задач и располагаются в одном месте. Посмотрите на пример использования FilesystemIterator
в сравнении с readdir()
. Оба метода применяют цикл, но в случае readdir()
вы обрабатываете только строку, а FilesystemIterator
работает с объектом, который может содержать дополнительную информацию о файле или директории (размер, владелец, права доступа и так далее).
Конечно, PHP представляет возможность для получения такой информации с помощью функций,например filesize()
и fileowner().
Но PHP5 основан на использовании концепции ООП. Поэтому лучше использовать современные методы работы с языком программирования. На нашем сайте есть уроки, посвященные работе с итераторами.
Как уже сообщалось во водной части урока, мы рассмотрим использование FilesystemIterator
, RecursiveDirectoryIterator
и GlobIterator
. Первый наследуется от DirectoryIterator
, а остальные от FilesystemIterator
. Они все имеют один и тот же конструктор, который принимает два параметра:
$path
(обязательный): путь к пункту файловой системы, над которым совершаются операции$flags
(опциональный): один или несколько флагов, перечисленных в документации
Реальное различие в данных итераторах заключается в их использовании для навигации по заданному пути.
FilesystemIterator
Использовать FilesystemIterator
очень просто. Рассмотрим в действии. Представляем два примера. Первый показывает поиск всех файлов и каталогов, имена которых начинаются на “te”. Второй пример использует другой итератор RegexIterator
для поиска всех файлов и каталогов, имена которых заканчиваются на “t.dat” или “t.php”. Итератор RegexIterator
используется для фильтрации результата на основе регулярных выражений.
<?php $iterator = new FilesystemIterator("."); $filelist = array(); foreach($iterator as $entry) { if (strpos($entry->getFilename(), "te") === 0) { $filelist[] = $entry->getFilename(); } }
Выше приведенный код выдаст результат, аналогичный предыдущим примерам.
Второй пример с применением RegexIterator
:
<?php $iterator = new FilesystemIterator("."); $filter = new RegexIterator($iterator, '/t.(php|dat)$/'); $filelist = array(); foreach($filter as $entry) { $filelist[] = $entry->getFilename(); }
Он будет выводить:
array ( 0 => 'script.php', 1 => 'test.dat' )
RecursiveDirectoryIterator
Итератор RecursiveDirectoryIterator
обеспечивает интерфейс для рекурсивного прохода по директориям файловой системы. Он имеет несколько полезных методов, таких как getChildren()
и hasChildren()
, которые возвращают итератор для текущего места, если это директория, и проверяют, является ли текущая точка входа директорией. Следующий пример демонстрирует использование RecursiveDirectoryIterator
и getChildren()
. Результат будет такой же, как и в предыдущих примерах.
<?php $iterator = new RecursiveDirectoryIterator('.'); $filter = new RegexIterator($iterator->getChildren(), '/t.(php|dat)$/'); $filelist = array(); foreach($filter as $entry) { $filelist[] = $entry->getFilename(); }
GlobIterator
Итератор GlobIterator
выполняет проход по файловой системе также, как и функция glob()
. Первый параметр может включать шаблон для имени. Пример демонстрирует использование GlobIterator
с тем же результатом, что и ранее.
<?php $iterator = new GlobIterator("te*"); $filelist = array(); foreach($iterator as $entry) { $filelist[] = $entry->getFilename(); }
Заключение
В данном уроке демонстрируется использование различных подходов для достижение одинаковой цели: получение списка файлов и директорий. Следует запомнить следующие ключевые моменты:
- Функция
glob()
является встроенным решением, но она недостаточно гибкая. - Решение на основе
opendir()
,readdir()
, иclosedir()
более сложное и требует дополнительной фильтрации, но оно более гибкое. - Функция
scandir()
требует дополнительной фильтрации, но работает без обработки дескриптора. - Если вы используете подход ООП, то следует применять библиотеку SPL. Дополнительно вы можете расширить классы своим функционалом.
- Итератор
GlobIterator
имеет функцию предварительной фильтрации, а другие используютRegexIterator
.
Ready for copy and paste function for common use cases, improved/extended version of one answer above:
function getDirContents(string $dir, int $onlyFiles = 0, string $excludeRegex = '~/.git/~', int $maxDepth = -1): array {
$results = [];
$scanAll = scandir($dir);
sort($scanAll);
$scanDirs = []; $scanFiles = [];
foreach($scanAll as $fName){
if ($fName === '.' || $fName === '..') { continue; }
$fPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($dir . '/' . $fName));
if (strlen($excludeRegex) > 0 && preg_match($excludeRegex, $fPath . (is_dir($fPath) ? '/' : ''))) { continue; }
if (is_dir($fPath)) {
$scanDirs[] = $fPath;
} elseif ($onlyFiles >= 0) {
$scanFiles[] = $fPath;
}
}
foreach ($scanDirs as $pDir) {
if ($onlyFiles <= 0) {
$results[] = $pDir;
}
if ($maxDepth !== 0) {
foreach (getDirContents($pDir, $onlyFiles, $excludeRegex, $maxDepth - 1) as $p) {
$results[] = $p;
}
}
}
foreach ($scanFiles as $p) {
$results[] = $p;
}
return $results;
}
And if you need relative paths:
function updateKeysWithRelPath(array $paths, string $baseDir, bool $allowBaseDirPath = false): array {
$results = [];
$regex = '~^' . preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath($baseDir)), '~') . '(?:/|$)~s';
$regex = preg_replace('~/~', '/(?:(?!..?/)(?:(?!/).)+/..(?:/|$))?(?:.(?:/|$))*', $regex); // limited to only one "/xx/../" expr
if (DIRECTORY_SEPARATOR === '\') {
$regex = preg_replace('~/~', '[/\\\\]', $regex) . 'i';
}
foreach ($paths as $p) {
$rel = preg_replace($regex, '', $p, 1);
if ($rel === $p) {
throw new Exception('Path relativize failed, path "' . $p . '" is not within basedir "' . $baseDir . '".');
} elseif ($rel === '') {
if (!$allowBaseDirPath) {
throw new Exception('Path relativize failed, basedir path "' . $p . '" not allowed.');
} else {
$results[$rel] = './';
}
} else {
$results[$rel] = $p;
}
}
return $results;
}
function getDirContentsWithRelKeys(string $dir, int $onlyFiles = 0, string $excludeRegex = '~/.git/~', int $maxDepth = -1): array {
return updateKeysWithRelPath(getDirContents($dir, $onlyFiles, $excludeRegex, $maxDepth), $dir);
}
This version solves/improves:
- warnings from
realpath
when PHPopen_basedir
does not cover the..
directory. - does not use reference for the result array
- allows to exclude directories and files
- allows to list files/directories only
- allows to limit the search depth
- it always sort output with directories first (so directories can be removed/emptied in reverse order)
- allows to get paths with relative keys
- heavy optimized for hundred of thousands or even milions of files
- write for more in the comments
Examples:
// list only `*.php` files and skip .git/ and the current file
$onlyPhpFilesExcludeRegex = '~/.git/|(?<!/|.php)$|^' . preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath(__FILE__)), '~') . '$~is';
$phpFiles = getDirContents(__DIR__, 1, $onlyPhpFilesExcludeRegex);
print_r($phpFiles);
// with relative keys
$phpFiles = getDirContentsWithRelKeys(__DIR__, 1, $onlyPhpFilesExcludeRegex);
print_r($phpFiles);
// with "include only" regex to include only .html and .txt files with "/*_mails/en/*.(html|txt)" path
'~/.git/|^(?!.*/(|' . '[^/]+_mails/en/[^/]+.(?:html|txt)' . ')$)~is'
In this article, we’ll discuss how you can get a list of all the files in a directory in PHP.
In your day-to-day PHP development, you often need to deal with a file system—for example, getting a list of files in a specific directory. PHP provides a few different ways of reading the contents of a directory easily. Today, we’ll go through all these methods, along with examples to understand how each one works.
The opendir
, readdir
, and closedir
Functions
In this section, we’ll discuss the opendir
, readdir
, and closedir
functions to see how you can use them to get a list of files in a specific directory.
The opendir
function allows you to open up a directory handle, which you can use along with other functions for different operations on the directory. You need to pass the path to the directory in the first argument of the opendir
function. If the directory path is valid, a directory handle resource will be returned.
The readdir
function allows you to read a directory. You need to provide a valid directory handle in the first argument of the readdir
function, and you can iterate over all the entries and get a list of all files in a directory.
The closedir
function allows you to close the directory handle which is opened by the opendir
function. It’s a good practice to use the closedir
function once you’re done with your operations on the directory handle (which was initially opened by the opendir
function).
Now, let’s see it in action in the following example.
1 |
<?php
|
2 |
$arrFiles = array(); |
3 |
$handle = opendir('/path/to/directory'); |
4 |
|
5 |
if ($handle) { |
6 |
while (($entry = readdir($handle)) !== FALSE) { |
7 |
$arrFiles[] = $entry; |
8 |
}
|
9 |
}
|
10 |
|
11 |
closedir($handle); |
12 |
?>
|
Firstly, we’ve used the opendir
function to get the directory handle. Next, we’ve used the readdir
function to iterate over the $handle
directory handle and store a list of files in the $arrFiles
array.
The scandir
Function
In this section, we’ll see how you can use the scandir
function to get directory contents.
The scandir
function is a pretty straightforward way to get a list of files and directories in a specific directory. You just need to pass a directory path which you want to read in the first argument of the scandir
function.
Let’s go through the following example to understand how it works.
1 |
<?php
|
2 |
$arrFiles = scandir('/path/to/directory'); |
3 |
?>
|
As you can see, this provides a list of files and directories in a single call! If you just need to get a list of files in a directory, this is a better choice than using opendir
, readdir
, and closedir
.
The glob
Function
The glob
function works similarly to the scandir
function, with the difference that it allows you to specify a pattern for filtering and matching the files in the directory.
Let’s see how to use the glob
function to read all the contents of a specific directory.
1 |
<?php
|
2 |
$arrFiles = glob('/path/to/dir/*'); |
3 |
?>
|
As you can see, we’ve passed the *
pattern since we want to read all the contents. On the other hand, if you want to list only a specific type of file, you can do that as well, as shown in the following snippet.
1 |
<?php
|
2 |
$arrFiles = glob('/path/to/dir/*.txt'); |
3 |
?>
|
in this case, $arrFiles
would contain only the names of .txt
files.
The dir
Function
The options that we’ve discussed so far allow you to read directory contents in a procedural way. In this section, we’ll see how you can use the dir
function, which is an object-oriented mechanism for reading a directory.
When you use the dir
function, and pass a directory path in the first argument, it returns an instance of the Directory
class, which you can use subsequently to read the directory contents.
Let’s go through the following example to see how it works exactly.
1 |
<?php
|
2 |
$arrFiles = array(); |
3 |
$objDir = dir("/path/to/dir"); |
4 |
|
5 |
while (false !== ($entry = $objDir->read())) { |
6 |
$arrFiles[] = $entry; |
7 |
}
|
8 |
|
9 |
$objDir->close(); |
10 |
?>
|
First, we used the dir
function to initialize an instance of the Directory
class into the $objDir
variable. Next, we used the read
method of the Directory
class to iterate over all the entries.
The FilesystemIterator
Class
In this section, we’ll see how you can use the FilesystemIterator
class to read directory contents. The benefit of using the FilesystemIterator
class is that you can extend the methods of this class, and it also provides a lot of other useful methods that allow you to fetch various information about the files.
Let’s go through the following example to understand how it works.
1 |
<?php
|
2 |
$arrFiles = array(); |
3 |
$iterator = new FilesystemIterator("/path/to/directory"); |
4 |
|
5 |
foreach($iterator as $entry) { |
6 |
$arrFiles[] = $entry->getFilename(); |
7 |
}
|
8 |
?>
|
As you can see, it’s really easy to use the FilesystemIterator
class and iterate over the entries. In the above example, we’ve used the getFileName
method of the FilesystemIterator
class to get the file name. However, the FilesystemIterator
class also provides several utility methods like getExtension
, getSize
, getPathname
, and so on.
So that’s how you can use the FilesystemIterator
class to get a list of files in a directory.
Conclusion
Today, we discussed different ways of listing all the files in a directory in PHP. Here’s a table to compare each method to help you choose which one is right for you.
Function or Class | Notes |
---|---|
opendir , readdir and closedir functions |
not the simplest or most feature-rich method |
scandir function |
simplest way to read a file list |
glob function |
very simple and allows pattern matching |
dir function |
object-oriented version of readdir
|
FilesystemIterator class |
can provide other info about files, like size or permissions |
Did you find this post useful?
Software Engineer, FSPL, India
I’m a software engineer by profession, and I’ve done my engineering in computer science. It’s been around 14 years I’ve been working in the field of website development and open-source technologies.
Primarily, I work on PHP and MySQL-based projects and frameworks. Among them, I’ve worked on web frameworks like CodeIgnitor, Symfony, and Laravel. Apart from that, I’ve also had the chance to work on different CMS systems like Joomla, Drupal, and WordPress, and e-commerce systems like Magento, OpenCart, WooCommerce, and Drupal Commerce.
I also like to attend community tech conferences, and as a part of that, I attended the 2016 Joomla World Conference held in Bangalore (India) and 2018 DrupalCon which was held in Mumbai (India). Apart from this, I like to travel, explore new places, and listen to music!