Support Forum
Support › MB User Profile › Randomize File Name in Document UploadResolved
Hi, 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!
Hello,
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
Do 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?
Hello,
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 function ajf_randomize_uploaded_filename()
I'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;
}
Hello,
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/
Thank 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!