note 59000 deleted from function.glob by cmb
| From: | cmb@php.net | Date: | Sun, 28 Jul 2019 08:09:50 +0000 |
| Subject: | note 59000 deleted from function.glob by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211724@lists.php.net to get a copy of this message | ||
Note Submitter: admiral [at] nuclearpixel [dot] com
----
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!