Skip to content

Instantly share code, notes, and snippets.

@rozzy
Created June 21, 2013 12:47
Show Gist options
  • Save rozzy/5830921 to your computer and use it in GitHub Desktop.
Save rozzy/5830921 to your computer and use it in GitHub Desktop.
Recursively find all images mentions in all files
<?php
// Run with shell as `php -a scanimages.php`
// If you want to check your actual place, use `pwd`
define(PATH, "/Users/rozzy/Source/Web/mypath/");
$all_files = scandir(PATH);
$may_content = $matches = $end_f = array();
foreach ($all_files as $value) {
$exclude = array(
'.',
'..',
'.hg',
'!config'
);
if (is_file(PATH.$value) or in_array($value, $exclude))
continue;
array_push($may_content, $value);
}
foreach ($may_content as $folder) {
$it = new RecursiveDirectoryIterator(PATH.$folder);
foreach(new RecursiveIteratorIterator($it) as $file) {
if (preg_match('#public/images#i', $file)) continue; // doesn't touches images folder
array_push($end_f, $file);
}
}
foreach ($end_f as $file) {
$file_c = file_get_contents($file);
preg_match_all('#\/[^\"](\S+\.(svg|gif|png|jpg|jpeg))#s', $file_c, $all_matches);
foreach( $all_matches[0] as $path ) {
if (!in_array($path, $matches)) array_push($matches, $path);
}
}
foreach ($matches as $key => $value) {
$str .= "[$key] $value".PHP_EOL;
}
file_put_contents(PATH.'allimg.txt', $str); // keeps data in ~
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment