0

How do I make the old file replaced when the user uploads a new one?

Now the files are named a1.jpg, a2.jpg, a3.jpg. If the user uploads a.jpg, then I want to overwrite the a.jpg that already exists. This shall only apply to this folder / form.

I use ultimate member together with gravity forms and gravity view. Have a form where the owner of the role (company) can upload their own company logo. It gets the renamed to the user role and is placed in a special folder.

So far I've managed to do this:

  • Rename the file to the user's role - Done
  • Put the file in a dedicated folder - Done
  • If the image already exists, overwrite it - Not done

The full code can be found here: https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-gravity-forms-rename-uploaded-files.php

The configuration in functions

# Configuration

new GW_Rename_Uploaded_Files( array(
'form_id' => 10,
'field_id' => 6,
// most merge tags are supported, original file extension is preserved
'template' => '{Role:7}',
// Ignore extension when renaming files and keep them in sequence (e.g. a.jpg, a1.png, a2.pdf etc.)
'ignore_extension' => false,
) );

To put the files in the correct folder:

add_filter( 'gform_upload_path_10', 'change_upload_path', 10, 2 );
function change_upload_path( $path_info, $form_id ) {
    $path_info['path'] = '/var/www/vhosts/example.com/httpdocs/wp-content/uploads/company_logos/';
    $path_info['url'] = 'https://example.com/wp-content/uploads/company_logos/';
    return $path_info;
}

So far everything is great. The only problem I have is that when the user uploads or changes the image, it should overwrite the old one. Now when the user uploads a new one, they are named company.jpg, company1.jpg, company2.jpg etc.

How do I make the old file replaced?

UPDATE. I found this code for another form. Can I use it in some way for my function?

<?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
);

1 Answer 1

0

These lines of code and can help you to overwrite image


        $attachment_id = get_post_thumbnail_id($post_id);
        wp_delete_attachment($attachment_id, true);

        if(isset($file) && !empty($file) ){

            $upload_overrides = array('test_form'=> false);
            $movefile = wp_handle_upload($file, $upload_overrides);

            $filename = basename( $movefile['url'] );

            $wp_filetype = wp_check_filetype($filename, null );

            $attachment = array(
                'guid' => $wp_upload_dir['url'] . '/' . $filename,
                'post_mime_type' => $wp_filetype['type'],
                'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
                'post_content' => '',
                'post_status' => 'inherit',

            );

            $attach_id = wp_insert_attachment( $attachment, $movefile['url'], $post_id );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $movefile['url'] );
            // wp_update_attachment_metadata( $attach_id, $attach_data );
            set_post_thumbnail( $post_id, $attach_id );

            $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), "single-post-thumbnail")[0];
            update_post_meta($post_id, '_thumbnail_id', $attach_id);

            
            update_post_meta($post_id, '_thumbnail_id', $attach_id);
1
  • Unfortunately, I'm not very good at coding. I guess I have to edit this code somehow. Found this. If file exists Do Something? foreach ( $uploaded_files as $_file ) { if ( in_array( $_file, $stashed_files ) ) { $renamed_files[] = $_file; continue; } $dir = wp_upload_dir(); $dir = $this->get_upload_dir( $form['id'] ); $file = str_replace( $dir['url'], $dir['path'], $_file ); if ( ! file_exists( $file ) ) { continue; }
    – Masterior
    Jan 26, 2023 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.