-
Notifications
You must be signed in to change notification settings - Fork 0
/
camerafilesorganizer.php
395 lines (358 loc) · 10.4 KB
/
camerafilesorganizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/*
* PHP CLI interactive script for Linux to organize camera files
* move files into date folders: 2022 > 2022-01 > 2022-01-02
* specifically for NIKON cameras: DSC_XXXX.JPG/NEF
* reads EXIF DateTimeOriginal
*
* with dry-run option levels:
* 1 = no mkdir, chmod or move is done. just shows old and new paths
* 2 = everything done except actually moving the file
*
* logs output summary to text file and details, messages to CSV files
*
* @todo: MOV video files
*/
define('TEST_MODE', 0);
if (TEST_MODE)
print "\n -- TEST MODE: process first file only\n";
define('DEFAULT_DESTINATION', '');
define('DEFAULT_DESTINATION_NEF', '');
define('START_TIME', date("Y-m-d H:i:s"));
askDryRun();
if (DRY_RUN) {
askDryRunLevel();
} else {
define('DRY_RUN_LEVEL', 0);
}
askFileType();
$paths_arr = askPaths();
if (!validatePaths($paths_arr)) {
exit();
} else {
print "\n -- Checking paths... Looks good.\n";
}
// camera files absolute path to import
define('SRC_PATH', $paths_arr[0]);
// camera files absolute path destination, where YEAR folder will go under
define('DEST_PATH', $paths_arr[1]);
// fetch into array select filenames in src folder
$file_array = array();
foreach (glob(SRC_PATH.'/*.{'.FILE_TYPE.'}', GLOB_BRACE) as $filename) {
array_push($file_array, $filename);
}
$GLOBALS['summary_arr'] = $GLOBALS['details_arr'] = $GLOBALS['messages_arr'] = array();
$GLOBALS['success_count'] = $GLOBALS['error_count'] = 0;
processFiles($file_array);
csvLogger($GLOBALS['details_arr'], 'details.csv');
logSummary();
print "\n -- Moved {$GLOBALS['success_count']} file(s).\n";
print "\n -- DONE!\n";
function processFiles($file_array = array()) {
define('FILE_COUNT', count($file_array));
print "\n -- Processing " . FILE_COUNT . " file(s)...";
for ($i = 0; $i < FILE_COUNT; $i++) {
$arr_detail = array();
$oldfile_path = $file_array[$i];
$filename = basename($file_array[$i]);
$filetype = explode('.', $filename)[1];
$filesize = filesize($oldfile_path);
$filesize_pretty = formatFileSize(filesize($oldfile_path));
$photo_datetime = getDateTimeOriginal($oldfile_path); // 2022:01:22 19:34:55
if ($photo_datetime) {
$pdt_arr = explode(' ', $photo_datetime);
list($pic_year, $pic_month, $pic_day) = explode(':', $pdt_arr[0]);
$photo_ymd = implode('-', array($pic_year, $pic_month, $pic_day));
$photo_datetime_formatted = $photo_ymd . ' ' . $pdt_arr[1]; // 2022-04-26 09:23:40
} else {
continue;
}
$newdest_path = DEST_PATH . '/' . $pic_year . '/' . $pic_year . '-' . $pic_month . '/' . $pic_year . '-' . $pic_month . '-' . $pic_day;
$newfile_path = $newdest_path . '/' . $filename;
if (createFolders($newdest_path)) {
moveFile($oldfile_path, $newfile_path, $photo_datetime_formatted);
}
$arr_detail = array(
$filename,
$filetype,
$photo_ymd,
$oldfile_path,
$newfile_path,
$photo_datetime,
$filesize_pretty,
$filesize
);
logDetail($arr_detail);
if (TEST_MODE)
break;
}
}
function createFolders($path) {
if (pathExists($path)) {
if (!makePathWritable($path)) {
return false;
} else {
logMsg($path, 'path is good');
return true;
}
} else {
if (!DRY_RUN || DRY_RUN_LEVEL != 1) {
if (!mkdir($path, 0755, true)) {
logMsg($path, 'failed to create directories', 'error');
return false;
} else {
logMsg($path, 'mkdir success');
return true;
}
} else {
logMsg($path, 'skip mkdir on DRY_RUN_LEVEL 1');
return false;
}
}
}
function moveFile($old_path, $new_path, $photo_datetime_formatted) {
if (!DRY_RUN) {
if (file_exists($new_path)) {
logMsg($new_path, 'file already exists', 'error');
return false;
} else if (!rename($old_path, $new_path)) {
logMsg($new_path, 'failed to move file to this location', 'error');
return false;
} else {
// set file modified date to the photo's original date
exec('touch -m -d "' . $photo_datetime_formatted . '" ' . $new_path);
logMsg($new_path, 'Successfully moved file.');
$GLOBALS['success_count']++;
return true;
}
return false;
} else {
logMsg($new_path, 'skip move file on DRY-RUN');
return false;
}
}
function formatFileSize($filesize = 0) {
if ($filesize < 1073741824) {
$filesize_pretty = round($filesize / 1048576, 2) . ' MB';
} else {
$filesize_pretty = round($filesize / 1073741824, 2) . ' GB';
}
return $filesize_pretty;
}
function getDateTimeOriginal($file_path = '') {
$fp = fopen($file_path, 'rb');
if (!$fp) {
logMsg($file_path, 'unable to open image for reading', 'error');
return false;
}
// not the best solution but works for me, see https://stackoverflow.com/q/5184748
$headers = @exif_read_data($fp, 'EXIF', $as_arrays = true);
if (!$headers) {
logMsg($file_path, 'unable to read EXIF headers', 'error');
return false;
}
foreach ($headers['EXIF'] as $header => $value) {
if ($header == 'DateTimeOriginal') // DateTimeOriginal => 2022:01:22 19:34:55
return $value;
}
return false;
}
/*function getDateTimeOriginalNEF($file_path = '') {
$fp = fopen($file_path, 'rb');
if (!$fp) {
logMsg($file_path, 'unable to open image for reading', 'error');
return false;
}
$img = new Imagick($file_path);
$allProp = $img->getImageProperties();
$exifProp = $img->getImageProperties("exif:*");
print_r($allProp);
if(isset($exifProp['exif:DateTimeOriginal'])) {
return $exifProp['exif:DateTimeOriginal'];
} else {
logMsg($file_path, 'unable to read NEF EXIF DateTimeOriginal', 'error');
return false;
}
}*/
function pathExists($path) {
if (file_exists($path) && is_dir($path))
return true;
else
return false;
}
function makePathWritable($path, $mode = 'regular') {
if (is_writable($path)) {
return true;
} else {
if ($mode == 'regular') {
if (!DRY_RUN && DRY_RUN_LEVEL != 1) {
// try change directory permissions
if (!chmod($path, 0755)) {
// print "\n -- Path Error: $path -- Failed to change permissions.\n";
logMsg($path, 'chmod failed to change permissions', 'error');
return false;
}
} else {
// print "\n -- Path: $path -- Skipping chmod for DRY_RUN_LEVEL 1\n";
logMsg($path, 'skip chmod on DRY_RUN_LEVEL 1');
return false; // skip chmod for DRY_RUN_LEVEL 1
}
} else { // i.e. validate_only
logMsg($path, 'skipped chmod');
return false;
}
}
}
function validatePaths($paths = array()) {
if ($paths[0] == $paths[1]) {
print "\n -- Path Error: Source and Destination cannot be the same.\n";
return false;
}
for ($i = 0; $i < count($paths); $i++) {
if (!pathExists($paths[$i])) {
print "\n -- Path Error: $paths[$i] -- does not exist.\n";
return false;
}
if (!makePathWritable($paths[$i], 'validate_only')) {
print "\n -- Path Error: $paths[$i] -- is not writable.\n";
return false;
}
}
return true;
}
function askDryRun() {
do {
$input = trim(strtolower( readline("\n> Dry Run? (y/n) ") ));
readline_add_history($input);
switch ($input) {
case 'y':
define('DRY_RUN', true);
print "\n -- DRY-RUN Mode is ON\n";
break;
case 'n':
define('DRY_RUN', false);
print "\n -- Okay. NOT on Dry-Run Mode!\n";
break;
default:
print "\n -- please input 'y' or 'n'\n";
break;
}
} while ( !in_array($input, array('y', 'n')) );
}
function askDryRunLevel() {
$ask = <<<EOD
\n> Dry-Run Level? (1 or 2)
\n 1 = no mkdir, chmod or move is done. just shows old and new paths
\n 2 = everything done except actually moving the file
\n>
EOD;
do {
$input = trim(strtolower( readline($ask . " ") ));
readline_add_history($input);
switch ($input) {
case '1':
define('DRY_RUN_LEVEL', 1);
print "\n -- DRY-RUN Level 1";
print "\n -- no mkdir, chmod or move will be done. just show old and new paths\n";
break;
case '2':
define('DRY_RUN_LEVEL', 2);
print "\n -- DRY-RUN Level 2";
print "\n -- everything will be done except actually moving the file\n";
break;
default:
print "\n -- please input '1' or '2'\n";
break;
}
} while ( !in_array($input, array('1', '2')) );
}
function askFileType() {
do {
$input = trim(strtolower( readline("\n> File Type? (j = JPEG, n = NEF) ") ));
readline_add_history($input);
switch ($input) {
case 'j':
define('FILE_TYPE', 'JPG');
print "\n -- searching JPEG files\n";
break;
case 'n':
define('FILE_TYPE', 'NEF');
print "\n -- searching NEF files\n";
break;
default:
print "\n -- please select: j = JPEG, n = NEF\n";
break;
}
} while ( !in_array($input, array('j', 'n')) );
}
function askPaths() {
$paths_arr = array();
print "\nEnter absolute paths.\n";
do {
$input = trim(readline("\n> Source Path: "));
readline_add_history($input);
if (strlen(trim($input)) > 0)
array_push($paths_arr, rtrim($input, '/'));
} while ( strlen(trim($input)) == 0 );
if (FILE_TYPE == 'NEF')
$def_dest = DEFAULT_DESTINATION_NEF;
else
$def_dest = DEFAULT_DESTINATION;
if (strlen(trim($def_dest)) > 0) {
print "\n -- DEFAULT DESTINATION: " . $def_dest;
$input = trim(strtolower( readline("\n> Use Default? (y/n) ") ));
readline_add_history($input);
switch ($input) {
case 'y':
array_push($paths_arr, rtrim($def_dest, '/'));
return $paths_arr;
default:
break;
}
}
do {
$input = trim(readline("\n> Destination Path (YEAR folder will go under this): "));
readline_add_history($input);
if (strlen(trim($input)) > 0)
array_push($paths_arr, rtrim($input, '/'));
} while ( strlen(trim($input)) == 0 );
return $paths_arr;
}
function logMsg($item = '', $message = '', $type = 'info') {
array_push($GLOBALS['messages_arr'], array($item, $type, $message));
if ($type == 'error')
$GLOBALS['error_count']++;
}
function logDetail($arr = array()) {
array_push($GLOBALS['details_arr'], $arr);
}
function csvLogger($arr = array(), $filename = 'output.csv') {
$csvfile = fopen($filename, 'w');
foreach ($arr as $line) {
fputcsv($csvfile, $line);
}
fclose($csvfile);
}
function logSummary() {
list($start, $file_type, $src, $dest, $dryrun, $level, $file_count) = array(
START_TIME, FILE_TYPE, SRC_PATH, DEST_PATH,
!DRY_RUN ? 'No' : 'Yes',
!DRY_RUN ? '0' : DRY_RUN_LEVEL,
FILE_COUNT
);
$end = date("Y-m-d H:i:s");
$summary = <<<SUMMARY
Start Time: {$start}\n
End Time: {$end}\n
File Type: {$file_type}\n
Source: {$src}\n
Destination: {$dest}\n
Dry-Run? {$dryrun}\n
Dry-Run Level: {$level}\n
File to Process: {$file_count}\n
Files Moved: {$GLOBALS['success_count']}\n
Errors: {$GLOBALS['error_count']}\n
SUMMARY;
file_put_contents('summary.txt', $summary);
csvLogger($GLOBALS['messages_arr'], 'messages.csv');
}