public
Last active

  • Download Gist
get-files.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
 
function get_files( $source_directory, $extensions = 'jpg' ) {
$directory = new DirectoryIterator( $source_directory );
$files = new ExtensionFilter( $directory, (array)$extensions );
 
return $files;
}
 
class ExtensionFilter extends FilterIterator {
protected $extensions;
 
function __construct( Iterator $iterator, $extensions ) {
parent::__construct( $iterator );
$this->extensions = $extensions;
}
 
public function accept() {
return in_array( pathinfo( $this->current()->getFilename(), PATHINFO_EXTENSION ),
$this->extensions );
}
}

What are the benefits of this process over the one used in David Walsh's example - http://davidwalsh.name/generate-photo-gallery/

I think the main advantage is that it's much clearer and the code is more modular and maintainable. If you're doing this just for one gig it doesn't really matter what you use though.

Thanks for the answer.

Substituting this into David Walsh's example seems to work just fine, but it prevents sorting the results using code suggested in this comment: http://davidwalsh.name/generate-photo-gallery#comment-17911

What would need to be done differently for this?

I don't know if it has anything to do with the OS or FS, but for me the files are always sorted (Windows, NTFS).

Now, testing it on Ubuntu, ext4, it indeed doesn't sort automagically. I'll see what I can do.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.