• Resolved deniscraciun

    (@deniscraciun)


    Hi everyone! I have a problem that I don’t seem to find anywhere.
    I have created a script that uploads images from an AWS bucket to the WordPress uploads folder. However, whenever I upload an image that gets overwritten (because it has the same image name), it becomes a duplicated in the media section, even tho the image only exists once in the uploads folder. So what is created is indeed a fake duplicate, also because if I delete (from the WordPress media section) the original images and leave the duplicates, which don’t work since they don’t in reality exist, and they are just there on the front end. I thought it would be a caching problem, but I tried deleting it, and also restarting my WordPress container (since I host it on a pc with Docker), and they are still there! In my script I do use a piece of code that triggers the library update also (so that I can see when I uploaded new images), and I’ll leave that there in case someone more expert than me can figure out if there’s a problem with that part. Thank you!!

    // Trigger media library update
    
    $wp_filetype = wp_check_filetype($file_name, null);
    
    $attachment = [
    
    'guid' => $upload_dir['url'] . '/' . $file_name,
    
    'post_mime_type' => $wp_filetype['type'],
    
    'post_title' => sanitize_file_name($file_name),
    
    'post_content' => '',
    
    'post_status' => 'inherit',
    
    ];
    
    $attach_id = wp_insert_attachment($attachment, $upload_path);
    
    require_once ABSPATH . 'wp-admin/includes/image.php';
    
    $attach_data = wp_generate_attachment_metadata($attach_id, $upload_path);
    
    wp_update_attachment_metadata($attach_id, $attach_data);

    • This topic was modified 1 year, 5 months ago by deniscraciun.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @deniscraciun,

    Since we aren’t fully sure about how the given snippet is supposed to work, what we could comment is limited as the behaviour noticed is more specific to the custom code.

    However, from the given code, most probably the following line is causing the issue:

    $attach_id = wp_insert_attachment($attachment, $upload_path);
    

    Where even if the file has the same name or not the mentioned line will create a new attachment entry in the media library, which leads to duplicate entries.

    You’ll have to add a check to see if the image exists before the wp_insert_attachment is called. You could use wp_update_attachment_metadata function to update the image that already exists.

    Please do check and see whether it would help in moving forward.

    Kind Regards,

    Nithin

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @deniscraciun,

    Since we haven’t heard from you for a while. I’ll mark this thread as resolved for now. Please feel free to re-open the thread if you need further assistance.

    Best Regards
    Nithin

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Fake duplicate images’ is closed to new replies.