note 46872 modified in function.scandir by danbrown

From: Date: Thu, 28 Jan 2010 14:17:16 +0000
Subject: note 46872 modified in function.scandir by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165450@lists.php.net to get a copy of this message
scandir() with regexp matching on file name and sorting options based on stat(). <?php 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 --was-- 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 http://php.net/manual/en/function.scandir.php

« previous php.notes (#165450) next »