Skip to content

Instantly share code, notes, and snippets.

@piersdavidson
Last active Jul 2, 2019
Embed
What would you like to do?
Filerun - rename iPhone photos/videos on upload
<?php
$supported_image = array(
'gif',
'jpg',
'jpeg',
'png' //,
//'mov' //can't pull created date for video files
);
$ext = strtolower(pathinfo($data['full_path'], PATHINFO_EXTENSION));
$result = preg_match('/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/', $data['full_path'], $matches, PREG_OFFSET_CAPTURE);
if ($result && in_array($ext, $supported_image)){
$exif = exif_read_data($data['full_path'], 0, true);
if ($exif) {
if(array_key_exists('DateTimeOriginal', $exif['EXIF']) ) {
$takenDate = new DateTime( $exif['EXIF']['DateTimeOriginal'] );
$oldfileName = pathinfo($data['full_path'], PATHINFO_FILENAME);
$newfileName = $takenDate->format('Y-m-d His');
$newfilePath = str_replace($oldfileName,$newfileName,$data['full_path']);
$newrelativePath= str_replace($oldfileName,$newfileName,$data['relative_path']);
$fileExists = file_exists($newfilePath);
if ($fileExists){
$oldfileSize = filesize($data['full_path']);
$newfileSize = filesize($newfilePath);
if ($oldfileSize != $newfileSize){
$newfileName = $newfileName . "_1";
}
}
$prepData = \FileRun\Files\Actions\Rename::prepare($data['relative_path'], $newfileName . "." . $ext);
if (!$prepData) { //if error, clean up & remove uploaded file
$error = \FileRun\Files\Actions\Rename::getError()['public'];
$prepData = \FileRun\Files\Actions\Delete\Prepare::prepare($data['relative_path']);
if (!$prepData) {
$error = \FileRun\Files\Actions\Delete\Prepare::getError()['public'];
}
else{
$rs = \FileRun\Files\Actions\Delete\File::run($prepData,true);
}
}
else{//proceed with rename
$rs = \FileRun\Files\Actions\Rename::run($prepData);
if (!$rs) {
$error = \FileRun\Files\Actions\Rename::getError()['public'];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment