Created
August 11, 2014 12:02
-
-
Save stephen-hill/6c483641ef209fb63d2b to your computer and use it in GitHub Desktop.
Copy Photos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copy Photos | |
* | |
* I made this script to copy photographs from my memory card to my network storage device. | |
* | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2014 Stephen Hill <stephen@gatekiller.co.uk> | |
* | |
* 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. | |
* | |
* @author Stephen Hill <stephen@gatekiller.co.uk> | |
* @copyright 2014 Stephen Hill <stephen@gatekiller.co.uk> | |
* @license http://www.opensource.org/licenses/MIT The MIT License | |
*/ | |
date_default_timezone_set("UTC"); | |
// Some basic input validation | |
$stderr = fopen('php://stderr', 'w'); | |
if ($argc < 3) | |
{ | |
fwrite($stderr, 'Please specific both the source and destination directory.' . PHP_EOL); | |
die; | |
} | |
$source = $argv[1]; | |
$destination = $argv[2]; | |
if (is_dir($source) === false) | |
{ | |
fwrite($stderr, 'The source directory does not exist.' . PHP_EOL); | |
fclose($stderr); | |
die; | |
} | |
if (is_dir($destination) === false) | |
{ | |
fwrite($stderr, 'The destination directory does not exist.' . PHP_EOL); | |
fclose($stderr); | |
die; | |
} | |
// We only want to copy these file types | |
$validExtensions = ['jpg' => true, 'jpeg' => true, 'nef' => true]; | |
// We'll use this the count how many photos we successfully copied | |
$copied = 0; | |
// This is the interator for the source directory | |
$directory = new DirectoryIterator($source); | |
// Now let's loop over the files in the source directory | |
// and copy the files we want. | |
foreach ($directory as $file) | |
{ | |
if (isset($validExtensions[strtolower($file->getExtension())]) === true) | |
{ | |
// Get and format the creation time | |
$dateTime = new DateTime('@' . $file->getCTime()); | |
$dateString = $dateTime->format('jS F Y'); | |
// Specify the new target directory and file path | |
$targetDirectory = $destination . '\\' . $dateString; | |
$targetFile = $targetDirectory . '\\' . $file->getFilename(); | |
// Create the target directory if it doesn't exist | |
if (is_dir($targetDirectory) === false) | |
{ | |
mkdir($targetDirectory); | |
} | |
// Copy the file if the target doesn't exist | |
if (file_exists($targetFile) === false) | |
{ | |
$copyResult = copy($file->getPathname(), $targetFile); | |
// Let's try to be sure the file copied | |
if ($copyResult === true) | |
{ | |
$sourceHash = hash_file('sha1', $file->getPathname()); | |
$destinationHash = hash_file('sha1', $targetFile); | |
if ($sourceHash === $destinationHash) | |
{ | |
$copied++; | |
} | |
else | |
{ | |
fwrite($stderr, "{$file->getFilename()} copied but SHA1 hash don't match." . PHP_EOL); | |
} | |
} | |
else | |
{ | |
fwrite($stderr, "{$file->getFilename()} failed to copy." . PHP_EOL); | |
} | |
} | |
} | |
} | |
// Echo how many photos we copied | |
echo $copied . ' photos copied.' . PHP_EOL; | |
fclose($stderr); | |
// | |
echo 'Press any key to continue.' . PHP_EOL; | |
$handle = fopen ("php://stdin","r"); | |
fgets($handle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment