Created
December 9, 2011 05:34
-
-
Save moimikey/1450332 to your computer and use it in GitHub Desktop.
An alternative to glob() using SPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Some words of wisdom: | |
* http://php.net/manual/en/class.directoryiterator.php | |
*/ | |
class HertzbergFilterDots extends FilterIterator { | |
public function __construct( $path ) { | |
parent::__construct( new DirectoryIterator( $path ) ); | |
} | |
public function accept() { | |
return ! $this->getInnerIterator()->isDot(); | |
} | |
} | |
$files = new HertzbergFilterDots( dirname( __FILE__ ) ); // ignore . and .. | |
/** | |
* Mayhaps you only want these file extensions... | |
*/ | |
$types = array( 'php', 'txt', 'pdf', 'doc' ); | |
foreach( $files as $file ) { | |
/* | |
if( ! in_array( $file->getExtension(), $types ) ) | |
continue; | |
*/ | |
$size = $file->getSize(); | |
$kb = round( ( $size / 1024 ), 2) . 'KB'; | |
$mb = round( ( $size / 1048576 ), 2) . 'MB'; | |
$gb = round( ( $size / 1073741824 ), 2) . 'GB'; | |
echo $file->getFilename() . ' (' . $kb . ')' . '<br />' . PHP_EOL; // cleaner source view | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment