Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Last active September 16, 2025 18:15
Show Gist options
  • Save BhargavBhandari90/6141d93412f82b48cce0716ad2b93196 to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/6141d93412f82b48cce0716ad2b93196 to your computer and use it in GitHub Desktop.
Iterate Images form uploads folder
<?php
// Scan images from uoloads folder including sub-folders.
function fp_scan_images() {
try {
$images = [];
$upload_dir = wp_upload_dir();
$directory = $upload_dir['basedir'];
// Recursive scan with FileSystemIterator
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $directory, FilesystemIterator::SKIP_DOTS )
);
if(empty($iterator)) return;
foreach ( $iterator as $file ) {
if ( $file->isFile() && in_array( strtolower( $file->getExtension() ), [ 'jpg', 'jpeg', 'png' ], true ) ) {
$images[] = str_replace( $directory, $upload_dir['baseurl'], $file->getPathname() );
}
}
$images = ! empty( $images ) ? array_unique( $images ) : [];
return $images;
} catch ( Exception $e ) {
error_log( 'FlyingPress: Error scaning images - ' . $e->getMessage() );
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment