Skip to content

Instantly share code, notes, and snippets.

@piersdavidson
Last active July 2, 2019 12:06
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 piersdavidson/483192914bece25fb4ebbd060c38297f to your computer and use it in GitHub Desktop.
Save piersdavidson/483192914bece25fb4ebbd060c38297f to your computer and use it in GitHub Desktop.
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