note 46872 added to function.scandir

From: Date: Tue, 26 Oct 2004 16:05:57 +0000
Subject: note 46872 added to function.scandir
Groups: php.notes 
Request: Send a blank email to php-notes+get-79131@lists.php.net to get a copy of this message
scandir() with regexp matching on file name and sorting options based on stat(). function myscandir($dir, $exp, $how='name', $desc=0) { $r = array(); $dh = @opendir($dir); if ($dh) { while (($fname = readdir($dh)) !== false) { if (preg_match($exp, $fname)) { $stat = stat("$dir/$fname"); $r[$fname] = ($how == 'name')? $fname: $stat[$how]; } } closedir($dh); if ($desc) { arsort($r); } else { asort($r); } } return(array_keys($r)); } $r = myscandir('./book/', '/^article[0-9]{4}\.txt$/i', 'ctime', 1); print_r($r); files can be sorted on name and stat() attributes, ascending and descending: name file name dev device number ino inode number mode inode protection mode nlink number of links uid userid of owner gid groupid of owner rdev device type, if inode device * size size in bytes atime time of last access (Unix timestamp) mtime time of last modification (Unix timestamp) ctime time of last inode change (Unix timestamp) blksize blocksize of filesystem IO * blocks number of blocks allocated ---- Manual Page -- http://www.php.net/manual/en/function.scandir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46872 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46872&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46872&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46872&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46872&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46872&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46872&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46872&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#79131) next »