• Resolved fotoclubchivasso

    (@fotoclubchivasso)


    Hi,

    I’ve created a form where users can upload up to 3 pictures along with a required Name Surname field.

    I have found a past discussion in which it is suggested to use a custom mu-plugin to accomplish that. I would like to append the Name and Surname contained in “name-1” to each of the uploaded files names, replacing spaces with _ (underscore)

    this is the code I am trying to use:

    <?php
    function wpmudev_modify_uploaded_file_names( $filename, $filename_raw ) {
        $info = pathinfo($filename);
        $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    
        if ( !empty( $_POST['name-1'] ) ) {
            $user_name = sanitize_text_field( $_POST['name-1'] );
            $filename = $user_name . '_' . basename($filename, $ext) . $ext; // Append user_name before the original filename
    	$filename = str_replace( ' ', '_',  $filename );
        
    }
    
        return $filename;
    }
    
    add_action( 'forminator_form_before_save_entry', 'wpmudev_uploaded_filename_check', 10, 1 );
    function wpmudev_uploaded_filename_check( $form_id ) {
    	if( $form_id == 202751 ) {
    		add_filter('sanitize_file_name', 'wpmudev_modify_uploaded_file_names', 10, 2);
    	}
    }

    the expected result is:
    name-1=John Doe
    Uploaded files: photo1.jpg, photo2.jpg, photo3.jpg
    Renamed files: John_Doe_photo1.jpg, John_Doe_photo2.jpg, John_Doe_photo3.jpg

    Can you help me to make it work?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @fotoclubchivasso,

    I hope you are doing well today!

    There are some points you need to take care of in the code;

    1. The custom function wpmudev_modify_uploaded_file_names is not returning $filename when $_POST['name-1'] is empty. Also, it lacks the process to update all the uploaded files with name-1.
    2. The filter is added after the form has been submitted (forminator_form_before_save_entry), so it might not affect the uploaded files’ names.

    Let’s try to revise your method as follows:

    <?php
    function wpmudev_modify_uploaded_file_names($filename, $filename_raw) {
        if (!empty($_POST['name-1'])) {
            $info = pathinfo($filename);
            $ext = empty($info['extension']) ? '' : '.' . $info['extension'];
            $user_name = sanitize_text_field($_POST['name-1']);
            $filename = $user_name . '_' . basename($filename, $ext) . $ext; // Append user_name before the original filename
            $filename = str_replace(' ', '_', $filename);
        }
    
        return $filename;
    }
    
    add_action('forminator_form_before_save_entry', 'wpmudev_uploaded_filename_check', 10, 1);
    function wpmudev_uploaded_filename_check($form_id) {
        if($form_id == 202751) {
            remove_all_filters('sanitize_file_name');
            add_filter('sanitize_file_name', 'wpmudev_modify_uploaded_file_names', 10, 2);
        }
    }
    

    This revised method will first remove all existing filters attached to the ‘sanitize_file_name’ hook, thus ensuring that only your custom function is used to modify the file name.

    Always remember to replace 202751 with your actual form ID in the actual implementation.

    It’s crucial not to forget to remove the filter after the form is saved to prevent it from affecting other uploads on the site, so add another action:

    add_action('forminator_form_after_save_entry', function($form_id) {
        if($form_id == 202751) {
            remove_filter('sanitize_file_name', 'wpmudev_modify_uploaded_file_names', 10, 2);
        }
    }, 10, 1);
    

    Finally, since altering with code can have unexpected consequences, it’s always good to have backup and proceed with caution. If you’re not comfortable making these changes, consider seeking professional help.

    Kind regards,
    Zafer

    Thread Starter fotoclubchivasso

    (@fotoclubchivasso)

    I have updated the code as you suggested but I still get upload error.

    the previous code was working with the single file submission form block but was appending the submitter name together with the classic random string that forminator creates.

    Can you suggest how to make this work also with multiple file uploads?

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @fotoclubchivasso,

    Please try the following code snippet instead.

    <?php
    function wpmudev_modify_uploaded_file_name( $filename, $filename_raw ) {	
    	$uniq_id = substr($filename, 0, 12);
    	$filename = str_replace(  $uniq_id.'-', '', $filename );
    	
        return $filename;
    }
    
    add_action( 'forminator_form_before_save_entry', 'wpmudev_uploaded_filename_fix', 10, 1 );
    function wpmudev_uploaded_filename_fix( $form_id ) {
    	if ( $form_id != 6 ) {
    		return;
    	}
    
    	add_filter('sanitize_file_name', 'wpmudev_modify_uploaded_file_name', 10, 2);
    }

    Please make sure to change 6 in if ( $form_id != 6 with your Form ID.

    Kind regards,
    Zafer

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @fotoclubchivasso

    We haven’t heard from you in a while, I’ll go and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Kind Regards,
    Kris

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Rename uploaded files in multiple upload’ is closed to new replies.