note 73960 deleted from function.scandir by felipe

From: Date: Fri, 29 Aug 2008 23:26:45 +0000
Subject: note 73960 deleted from function.scandir by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-144004@lists.php.net to get a copy of this message
Note Submitter: banzaimonkey at gmail dot com ---- 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. */ ?>

« previous php.notes (#144004) next »