note 68856 added to function.scandir
| From: | beingmrkenny at gmail dot com | Date: | Sun, 13 Aug 2006 18:41:32 +0000 |
| Subject: | note 68856 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115868@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 85.116.0.130
Probable Submitter: 82.47.202.211
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/68856
Del: integrated -- https://master.php.net/note/delete/68856/integrated
Del: useless -- https://master.php.net/note/delete/68856/useless
Del: bad code -- https://master.php.net/note/delete/68856/bad+code
Del: spam -- https://master.php.net/note/delete/68856/spam
Del: non-english -- https://master.php.net/note/delete/68856/non-english
Del: in docs -- https://master.php.net/note/delete/68856/in+docs
Del: other reasons-- https://master.php.net/note/delete/68856
Reject -- https://master.php.net/note/reject/68856
Search -- https://master.php.net/manage/user-notes.php