Skip to content

Instantly share code, notes, and snippets.

@moimikey
Created December 9, 2011 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moimikey/1450332 to your computer and use it in GitHub Desktop.
Save moimikey/1450332 to your computer and use it in GitHub Desktop.
An alternative to glob() using SPL
<?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