wp-auto-upload icon indicating copy to clipboard operation
wp-auto-upload copied to clipboard

Image gets saved to file system, but incorrect image path is saved in media attachment

Open Jakob-Maudience opened this issue 5 years ago • 6 comments

I have a strange behavior when using this plugin, I have imported posts from a Squarespace blog. I import the posts, and the image paths before the plugin is activated are like this: https://images.squarespace-cdn.com/content/v1/55b3d4afe4b0edbb36a4b6ea/1552236764572-KT58HNL34WDKJYMWFJFX/ke17ZwdGBToddI8pDm48kLzw7P_nKYlGGC0MzqILKf17gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z4YTzHvnKhyp6Da-NYroOW3rv7sVmavAApj1cuSnuUyXzqqXjS3CfNDSuuf31e0tVEhpqFMoPGd0_ypppePcJDz-vqyAZGPnUfr_Ql19q21eghW3lgGaAnbOfVbqEfQJYA/Accounts+Payable300dpi--3.jpg?format=original

When I activate the wp-auto-upload plugin, the path is like this: .../uploads/2019/10AccountsPayable300dpi-3.jpg

The image is saved to disk under this path: .../uploads/2019/10/AccountsPayable300dpi-3.jpg

It should be uploads/year/month/image. Instead the path is saved as uploads/year/monthimage (no slash between month and image name)

What is strange is, the image path does get overwritten properly in the edited post, where the original image url got replaced with the imported image url. But since the image url doesn't get saved to the media attachment, you are unable to see the image in the media gallery (a blank thumbnail) and you can not use the image in a different post / page.

I'm using the latest version of the plugin from wordpress.org and the latest stable wordpress release ( 5.2.4 ) I have the classic editor plugin installed if that's relevant. OS is windows, server is Apache.

Jakob-Maudience avatar Oct 31 '19 21:10 Jakob-Maudience

I was able to get around this issue by temporarily disabling the "Organize my uploads into month- and year-based folders" setting.

Still keeping this issue open as the current plugin breaks with the default setting. (I'd wager this is some kind of regex weirdness going on)

Jakob-Maudience avatar Oct 31 '19 22:10 Jakob-Maudience

Just to help out people in the future who have the same issue: the "Organize my uploads into month-..." option is located under general WP Media settings (/wp-admin/options-media.php), not in this plugin-specific settings.

gsk990 avatar Apr 19 '20 15:04 gsk990

I found a solution, replace the following code https://github.com/airani/wp-auto-upload/blob/1305af69b07e828c5407e24e2d4c5485bf95899e/src/ImageUploader.php#L256-L260

by

$attach_id = wp_insert_attachment($attachment, $image['base_path'] . '/' . $image['filename'], $this->post['ID']);
if (!function_exists('wp_generate_attachment_metadata')) {
    include_once( ABSPATH . 'wp-admin/includes/image.php' );
}
$attach_data = wp_generate_attachment_metadata($attach_id, $image['base_path'] . '/' . $image['filename']);

Because $image['path'] = $image['base_path'] . DIRECTORY_SEPARATOR . $image['filename'], it make wrong path when inserting the attachments.

huuthai37 avatar Sep 05 '20 18:09 huuthai37

I couldn't test and debug this problem and I think probably it happens in Windows OS also it's not clear enough for me

I have some questions about this problem:

  1. It's problem only for attachment path?
  2. Every image urls have this problem or just for specific url?

airani avatar Oct 03 '21 20:10 airani

This is the same problem, the proposed solution on WP.org is working for me. It's not Windows dependent.

https://wordpress.org/support/topic/uploaded-images-not-showing-in-the-media-library/

simo1994 avatar Jan 12 '24 18:01 simo1994

If you need to sanitize your database, I ran this query, then the images start showing up correctly under WP -> Admin -> Media and in gallery posts.

This will only work on MariaDB. Make a database backup before you run it!

UPDATE wp_postmeta a
SET meta_value = REGEXP_REPLACE(a.meta_value, '20([0-9]{2})/([0-9]{2})', '20\\1/\\2/')
where exists (select *
            from wp_posts b where a.post_id=b.ID
            and b.post_type='attachment')
and a.meta_key='_wp_attached_file'
and a.meta_value  REPLACE(REGEXP_REPLACE(a.meta_value, '20([0-9]{2})/([0-9]{2})', '20\\1/\\2/'), '//','/');

Updating the _wp_attachment_metadata records will break things, do not update it. Only update the _wp_attached_file as done above.

simo1994 avatar Jan 12 '24 18:01 simo1994