• Resolved wp_user1

    (@wp_user1)


    Hey, is there a way to force overwriting of file uploads?

    For example:

    I have an upload form.

    If the same filename already exists, it should be overwritten when new upload is made.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello there @wp_user1

    Not sure that this is a good practice as file names can overlap between different users submitting the form.

    Despite that, I pinged our developers if there’s any available filter/hook that we could use to revert that. We’ll keep you posted here as soon as we’ve got some more insights. Your patience is highly appreciated!

    Thank you,
    Dimitris

    Thread Starter wp_user1

    (@wp_user1)

    We are using this form just for internal uploads, so it is okay to overwrite files ??
    Thanks!

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @wp_user1

    Could you try this hook as a mu-plugin:
    https://gist.github.com/patrickfreitasdev/68b3a63ec34f85b3e5bfdb5b896b8912
    and please update the script and add:
    wp_delete_file( $param[ 'path' ]. $_FILES[ 'upload-1' ][ 'name' ] );
    just before this code
    return $param;

    We recommend testing this on the dev/staging version first before putting it on the live site.

    Kind Regards,
    Kris

    Thread Starter wp_user1

    (@wp_user1)

    Perfect that seems to work! Thanks!

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello there @wp_user1

    I was reviewing this topic and I wanted to mention that the given snippet above also does something else, not sure if you already noticed that but we missed communicating it with you and I’m sorry about that.

    So the given code apart from overriding files with the same name also changes the Forminator upload directory to /uploads/form-ID/ for each different form (where “form-ID” is the actual form ID number).

    I’ve already pinged our Second Level Support team to review the snippet and provide an alternative with file overriding but without changing the uploads dir.

    Warm regards,
    Dimitris

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello again

    The following snippet should do the trick, without changing any folders.

    <?php
    add_filter(
    	'wp_unique_filename',
    	function( $file_name, $ext, $dir, $cb, $alt, $no ) {
    		if ( isset( $_POST[ 'action' ] ) && 'forminator_submit_form_custom-forms' === $_POST[ 'action' ] ) {
    			if ( $no ) {
    				$find          = '-' . $no . $ext;
    				$original_name = str_replace( $find, $ext, $file_name );
    				return $original_name;
    			} else {
    				return $file_name;
    			}
    		}
    
    		return $file_name;
    	},
    	10,
    	6
    );

    Thank you,
    Dimitris

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘File upload: force to overwrite with same filename’ is closed to new replies.