note 59000 added to function.glob
| From: | admiral | Date: | Wed, 23 Nov 2005 16:38:01 +0000 |
| Subject: | note 59000 added to function.glob | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98983@lists.php.net to get a copy of this message | ||
I've written a function that I've been using quite a lot over the past year or so.
I've built whole websites and their file based CMSs based on this one function, mostly because
(I think) databases are not as portable as groups of files and folders. In previous versions, I used
opendir and readdir to get contents, but now I can do in one line what used to take several. How?
Most of the work in the whole script is done by calling
glob("$dir/*")
Giving me an array containing the names of the items in the folder, minus the ones beginning with
'.', as well as the ones I specify.
<?php
/* alpharead version 3: This function returns an array containing the names of the files inside any
given folder, excluding files that start with a '.', as well as the filenames listed in
the '$killit' array. This array is sorted using the 'natural alphabetical'
sorting manner. If no input is given to the function, it lists items in the script's
interpreted folder. Version 3 fixes a MAJOR bug in version 2 which corrupted certain arrays with
greater than 5 keys and one of the supposedly removed filenames.
written by Admiral at NuclearPixel.com */
function alpharead3($dir){
if(!$dir){$dir = '.';}
foreach(glob("$dir/*") as $item){$sort[]= end(explode('/',$item));}
$killit = array('index.html', 'index.php', 'thumbs.db',
'styles.css');
$killcounter = 0;
foreach($sort as $sorteditem){
foreach($killit as $killcheck){
if(strtolower($sorteditem) == strtolower($killcheck))
{unset($sort[$killcounter]);}
}$killcounter++;}
if($sort){natsort($sort);}
foreach($sort as $item){$return[]= $item;}
if(!$return){return array();}
return $return;
}
//some basic usage
$folder = 'images';
foreach(alpharead3($folder) as $item)
{
echo '<img
src="'.$folder.'/'.$item.'"><br>'.$item."\n";
}
?>
Commens on this function are welcome!
----
Manual Page -- http://www.php.net/manual/en/function.glob.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59000
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59000&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59000&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59000&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59000&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59000&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59000&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59000&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59000&report=yes
Search -- http://master.php.net/manage/user-notes.php