Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Created September 15, 2025 10:46
Show Gist options
  • Save BhargavBhandari90/4645cfd92c0a2cee88e4fc0499a56ac0 to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/4645cfd92c0a2cee88e4fc0499a56ac0 to your computer and use it in GitHub Desktop.
Scan Images in Bacthes using filesystem in WordPress
<?php
// Scan images from uploads folder
function fp_scan_images($offset = 0, $batch_size = 500)
{
try {
$images = [];
$upload_dir = wp_upload_dir();
$directory = $upload_dir['basedir'];
// Recursive scan with FileSystemIterator
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)
);
// Limit interation
$iterator = new LimitIterator($iterator, $offset, $batch_size);
if (! empty($iterator)) {
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());
}
}
}
return ! empty($images) ? array_unique($images) : [];
} catch (Exception $e) {
error_log('FlyingPress: Cannot open uploads directory: ' . $e->getMessage());
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment