Randomize File Name in Document Upload
Support › MB User Profile › Randomize File Name in Document UploadResolved
- This topic has 6 replies, 2 voices, and was last updated 2 years, 1 month ago by
lighterdayz.
-
AuthorPosts
-
February 21, 2023 at 10:05 PM #40648
lighterdayz
ParticipantHi, I am setting up a user profile where the user will upload their resume/cv document or pdf to /uploads/resume/. I would like to randomize the name of the file so that it can't be guessed. I'd like to use something like below, but only for this "/uploads/resume/" directory. How can I target that directory? And would the below code work?
// randomize upload filenames function ajf_randomize_uploaded_filename( $filename ) { // Add extension $ext = empty( pathinfo( $filename )['extension'] ) ? '' : '.' . pathinfo( $filename )['extension']; // return the first 10 characters of the MD5 hash of the name, along with the extension return substr(md5($filename), 0, 10) . $ext; } add_filter( 'sanitize_file_name', 'ajf_randomize_uploaded_filename', 10 );
Thanks!
February 22, 2023 at 6:29 PM #40667Peter
ModeratorHello,
You can use the field
file
to use the custom upload folder for uploaded files. Please read more on the documentation https://docs.metabox.io/fields/file/For example:
'upload_dir' => WP_CONTENT_DIR . '/uploads/resume/',
If you want to create a custom callback function to generate a unique file name, please refer to this topic https://support.metabox.io/topic/dynamic-uploaded-filename
February 23, 2023 at 1:01 AM #40685lighterdayz
ParticipantDo you mean unique does what I need? It will create a hash instead of the actual file name plus 1, 2, etc?
I've already set up the field in the builder and already created/directed to the directory.
I need a randomized file name based on MD5 hash so people can't guess it. Doesn't "unique" mean that is just adds a number to the end. I need the original file name sanitized and added as the md5 hash.
Can you help with that?
February 23, 2023 at 5:43 PM #40696Peter
ModeratorHello,
Yes, I think it is what you need. You can add md5() hash function or anything to the file name like this reply https://support.metabox.io/topic/dynamic-uploaded-filename/?swcfpc=1#post-34422
The setting
unique_filename_callback
helps you to execute a callback function like your functionajf_randomize_uploaded_filename()
February 28, 2023 at 7:27 AM #40753lighterdayz
ParticipantI'm still not completely clear how to do this. (read I don't know what I'm doing here.) I'm using the builder.
The file field is a User Profile field.(So, "Post Meta" and "post-meta"?)
I don't see a text domain. (is that field group?)Field title is "resume".
Can you correct this?
Thanks!---------------------------------
function ajf_unique_filename_callback( $dir, $name, $ext ) { $name = $name; return substr(md5($filename), 0, 10) . $ext; } add_filter( 'rwmb_meta_boxes', 'ajf_randomize_uploaded_filename' ); function ajf_randomize_uploaded_filename( $meta_boxes ) { $meta_boxes[] = [ <strong> 'title' => __( 'Post Meta', 'your-text-domain' ), 'id' => 'post-meta',</strong> 'fields' => [ [ 'name' => __( 'File', 'resume' ), 'id' => 'file', 'type' => 'file', 'upload_dir' => WP_CONTENT_DIR . '/uploads/resumes/', 'unique_filename_callback' => 'my_unique_filename_callback', ], ], ]; return $meta_boxes; }
February 28, 2023 at 6:27 PM #40756Peter
ModeratorHello,
Please check this code for example:
function my_unique_filename_callback( $dir, $name, $ext ) { $name = substr(md5($name), 0, 10); return $name . $ext; } add_filter( 'rwmb_meta_boxes', 'ajf_randomize_uploaded_filename' ); function ajf_randomize_uploaded_filename( $meta_boxes ) { $meta_boxes[] = [ 'title' => __( 'My User Meta', 'your-text-domain' ), 'id' => 'my-user-meta', 'type' => 'user', 'fields' => [ [ 'name' => __( 'File', 'resume' ), 'id' => 'file', 'type' => 'file', 'upload_dir' => WP_CONTENT_DIR . '/uploads/resumes/', 'unique_filename_callback' => 'my_unique_filename_callback', ], ], ]; return $meta_boxes; }
If you don't know how to use the code to register custom fields and meta box, please read more on the documentation https://docs.metabox.io/creating-fields-with-code/
February 28, 2023 at 9:34 PM #40759lighterdayz
ParticipantThank you! I was able to create the text domain on the "get php code" button on the field group. I will try this.
I appreciate the help very much!
-
AuthorPosts
- You must be logged in to reply to this topic.