note 73960 added to function.scandir
| From: | banzaimonkey at gmail dot com | Date: | Sun, 18 Mar 2007 03:24:48 +0000 |
| Subject: | note 73960 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124548@lists.php.net to get a copy of this message | ||
This script will scan a selected directory and return a list of subdirectories. It will not return
files in the list. The subdirectories will be returned in alphabetical order. Also, it will return
only the first level of subdirectories. This script is intended to be used in automated site
navigation.
<?php
/*
* We'll begin the script by telling PHP which directory to look
* at. The path should be a string, and hence should be enclosed
* in single quotes, such as 'home/door'
*/
$scanthis = '.';
/*
* scandir(); will list all of the files and directories in the
* specified directory, specified by $scanthis (above). In this
* line we're also initiating a new array into which we're placing
* the results of the $scandir function so it can be processed
* later.
*/
$scanlisting = scandir($scanthis);
/*
* Here we create a new array to be used in the next step. This
* array begins empty and will be filled by way of a foreach loop
* using values from our first array, $scanlisting.
*/
$dirlisting = array();
/*
* We will run through the list and transfer all of the directory
* names to a new array. The "as" will indicate the variable
* names by which we can reference the key or value in this array.
*/
foreach($scanlisting as $key => $value){
/*
* is_dir(); will tell you whether the selected path is a
* directory. If it is a directory, we want to add it to our new
* array.
*/
if (is_dir("$scanthis/$value") == true && $value != '.' &&
$value != '..'){
/*
* In order to append data to the array, we'll use empty square
* brackets. The name of the array is the new, empty array we.
* just created called $dirlisting. The value we are assigning
* to it is the variable defined in the foreach(); function.
*/
$dirlisting[] = $value;
}
}
/*
* If we want, we can output our array as a list of links.
*/
print('<ul>');
foreach($dirlisting as $key => $value){
print('<li><a href="' . $scanthis . '/' . $value .
'">' . $value . "</a></br></li>");
}
print('</ul>');
/*
* Please note that this code is not necessarily the most
* efficient method. However, I wanted to make things clear to a
* beginner where things were going and how they were getting
* there.
*/
?>
----
Server IP: 216.194.113.175
Probable Submitter: 68.108.249.145
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/73960
Del: integrated -- https://master.php.net/note/delete/73960/integrated
Del: useless -- https://master.php.net/note/delete/73960/useless
Del: bad code -- https://master.php.net/note/delete/73960/bad+code
Del: spam -- https://master.php.net/note/delete/73960/spam
Del: non-english -- https://master.php.net/note/delete/73960/non-english
Del: in docs -- https://master.php.net/note/delete/73960/in+docs
Del: other reasons-- https://master.php.net/note/delete/73960
Reject -- https://master.php.net/note/reject/73960
Search -- https://master.php.net/manage/user-notes.php