Skip to content

Instantly share code, notes, and snippets.

@danieltroger
Last active October 29, 2016 20:43
Show Gist options
  • Save danieltroger/cac42334b583ecff68d0addbd675d2f3 to your computer and use it in GitHub Desktop.
Save danieltroger/cac42334b583ecff68d0addbd675d2f3 to your computer and use it in GitHub Desktop.
Copy files from sd card and sort into Y/M/D folders
<?php
/*
The MIT License (MIT)
Copyright (c) 2016 Daniel Troger
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Usage: the code is obvious.
Example:
cd /A/dir/where/I/want/my/file/and/directory/structure
php sort.php "/Volumes/SDCARD/DCIM/100CANON/*"
*/
error_reporting(E_ALL);
date_default_timezone_set("Europe/Stockholm");
foreach(glob($argv[1]) as $f) {
$de = explode(".",$f);
$extkey = sizeof($de)-1;
$ext = $de[$extkey];
if(strtoupper($ext) !== "SRT" && strtoupper($ext) !== "LOG")
{
$d = datt($f);
$y = date("Y",$d);
$m = date("m",$d);
$da = date("d",$d);
unset($de[$extkey]);
$base = implode(".",$de);
$ymd = "{$y}/{$m}/{$da}/";
echo "{$ymd}, {$f}\n";
@mkdir($ymd,0755,true);
foreach(Array(".srt",".log") as $addfiles)
{
$src = $base . strtoupper($addfiles);
$src2 = $base . strtolower($addfiles);
if(file_exists($src)) rename($src, $ymd . basename($base) . strtoupper($addfiles));
else if(file_exists($src)) rename($src, $ymd . basename($base) . strtoupper($addfiles));
}
rename($f,$ymd . basename($f));
}
}
function datt($file)
{
$fdt = 0;
$video = explode("/",mime_content_type($file))[0] === "video";
if(!$video && function_exists("exif_read_data")) $exif = @exif_read_data($file);
if(isset($exif) && $exif !== FALSE && is_array($exif))
{
if(!empty($exif['DateTime']))
{
$fdt = strtotime($exif['DateTime']);
}
else
{
$fdt = $exif['FileDateTime'];
}
}
else if($video && ($dt = vdt($file)) !== false)
{
$fdt = $dt;
}
else
{
$fdt = filemtime($file);
}
return $fdt;
}
function vdt($file)
{
$file = str_replace("'","\'",$file);
$cr = explode(" ",explode("\n",shell_exec("ffprobe '{$file}' 2>&1|grep creation_time"))[0]);
if(sizeof($cr) < 8) return false;
$cr2 = Array($cr[8],$cr[9]);
return strtotime(implode(" ",$cr2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment