Skip to content

Instantly share code, notes, and snippets.

@CodeBrauer
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeBrauer/edab1838c0aa5b33cedd to your computer and use it in GitHub Desktop.
Save CodeBrauer/edab1838c0aa5b33cedd to your computer and use it in GitHub Desktop.
recursive directory batch file processing with shell exec
<?php
// not tested - just for logical demonstration
$path = [
'source' => '/source_files/',
'thumbnails' => '/public_html/images/',
];
function thumbnail_exists($file, $type) {
global $path;
// valid types: png, web, watermarked
$valid_types = ['png', 'web', 'watermarked'];
if (!in_array($type, $valid_types))
return [false, 'No valid type given'];
$file = str_replace($path['source'], $path['thumbnail'], $file);
return file_exists($file);
}
$objects = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path['source']),
RecursiveIteratorIterator::SELF_FIRST
);
// skip '.' and '..' in unix folders
$objects->setFlags(FilesystemIterator::SKIP_DOTS);
foreach ($objects as $name => $object) {
if ($object->isFile()) {
if (strtolower($object->getExtension()) === 'tif') {
if (!thumbnail_exists($object->getPathname(), 'png')) {
shell_exec('./convert.sh command_1');
}
if (!thumbnail_exists($object->getPathname(), 'web')) {
shell_exec('./convert.sh command_2');
}
if (!thumbnail_exists($object->getPathname(), 'watermarked')) {
shell_exec('./convert.sh command_3');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment