Skip to content

Instantly share code, notes, and snippets.

@codename065
Last active August 12, 2021 15:09
Show Gist options
  • Select an option

  • Save codename065/fa2d9befeccb8ad3b322cbc4cfb6d4a4 to your computer and use it in GitHub Desktop.

Select an option

Save codename065/fa2d9befeccb8ad3b322cbc4cfb6d4a4 to your computer and use it in GitHub Desktop.
Rename file after upload
<?php
/* Rename file */
add_action("wpdm_after_upload_file", function ($file_path) {
$dir = dirname($file_path);
$file = basename($file_path);
$ext = \WPDM\libs\FileSystem::fileExt($file);
$new_name = str_replace(".{$ext}", "-".uniqid().".{$ext}", $file);
$new_path = $dir.'/'.$new_name;
rename($file_path, $new_path);
echo "|||".$new_name."|||";
exit;
});
/* Organizing files in to sub-dirs by year */
add_action("wpdm_after_upload_file", function($path){
$filename = basename($path);
$upload_dir = dirname($path);
$year = date("Y");
$new_path = $upload_dir."/".$year."/".$filename;
$overwrite = (int)get_option('__wpdm_overwrrite_file',0);
if(file_exists($new_path) && $overwrite){
@unlink($new_path);
}
if(file_exists($new_path) && !$overwrite){
$filename = time().'wpdm_'.$filename;
$new_path = $upload_dir."/".$year."/".$filename;
}
if(!file_exists($upload_dir."/".$year."/"))
mkdir($upload_dir."/".$year."/");
rename($path, $new_path);
die("|||{$year}/{$filename}|||");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment