Last active
September 16, 2025 18:15
-
-
Save BhargavBhandari90/6141d93412f82b48cce0716ad2b93196 to your computer and use it in GitHub Desktop.
Iterate Images form uploads folder
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 | |
| // 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