Last active
August 12, 2021 15:09
-
-
Save codename065/fa2d9befeccb8ad3b322cbc4cfb6d4a4 to your computer and use it in GitHub Desktop.
Rename file after upload
This file contains hidden or 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 | |
| /* 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