Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active August 26, 2018 18:30
Show Gist options
  • Save vijinho/630a2a49ef19a6833a350ce04d1c5f5a to your computer and use it in GitHub Desktop.
Save vijinho/630a2a49ef19a6833a350ce04d1c5f5a to your computer and use it in GitHub Desktop.
get all files (and optionally dirs) in a dir path
#!/usr/bin/php
<?php
// get all files in a dir path,
function get_all_files($dir, $ignore_dirs = true) {
static $alldirs = array();
$dirs = glob($dir . '/*');
if (count($dirs) > 0) {
foreach ($dirs as $newdir) {
if (!array_key_exists($newdir, $alldirs)) {
$alldirs[$newdir] = $newdir;
}
}
}
foreach ($dirs as $dir) {
get_all_files($dir);
}
foreach ($alldirs as $k => $v) {
if ($ignore_dirs && is_dir($k)) {
unset($alldirs[$k]);
}
}
natcasesort($alldirs);
return $alldirs;
}
// set the file time to minimum of
// access time, creation time, modified time
$files = get_all_files('/Users/vijay/Pictures');
foreach ($files as $k => $file) {
echo "\n$file";
$ctime = min(filectime($file), filemtime($file), fileatime($file));
touch($file, $ctime, $ctime);
echo "\n\t" . date('r', $ctime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment