Skip to content

Instantly share code, notes, and snippets.

@ninsuo
Last active February 23, 2016 07:53
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 ninsuo/43b9c8125a0e5bd32ab8 to your computer and use it in GitHub Desktop.
Save ninsuo/43b9c8125a0e5bd32ab8 to your computer and use it in GitHub Desktop.
Tidy up pictures from a camera (generated path format: Y-m-d/H:i:s.ext)
<?php
# Run me inside a directory full of pictures
# Create an alias for easier use!
date_default_timezone_set('Europe/Paris');
$path = $_SERVER['PWD'];
$glob = glob("/{$path}/*");
$array = array ();
foreach ($glob as $file)
{
$mtime = filemtime($file);
$date = date("Y-m-d H:i:s", $mtime);
if (array_key_exists($date, $array) == false)
{
$array[$date] = 0;
}
$array[$date]++;
}
foreach ($glob as $file)
{
if (!is_file($file))
{
continue;
}
$mtime = filemtime($file);
$directory = date("Y-m-d", $mtime);
if (!is_dir($directory))
{
mkdir($directory);
}
$date = date("Y-m-d H:i:s", $mtime);
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($array[$date] == 1)
{
$newName = date("H-i-s", $mtime) . "." . $ext;
$newPath = "{$directory}/{$newName}";
echo "New name: {$newPath}\n";
rename($file, $newPath);
}
else
{
for ($count = 1; ($count <= $array[$date]); $count++)
{
$newName = date("H-i-s", $mtime) . "_{$count}." . $ext;
$newPath = "{$directory}/{$newName}";
if (!is_file($newPath))
{
echo "New name: {$newPath}\n";
rename($file, $newPath);
break ;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment