Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Jakob-Maudience opened this issue Oct 31, 2019 · 6 comments

Comments

@Jakob-Maudience
Copy link

Jakob-Maudience commented Oct 31, 2019

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
Copy link
Author

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)

@gsk990
Copy link

gsk990 commented Apr 19, 2020

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.

@huuthai37
Copy link

huuthai37 commented Sep 5, 2020

I found a solution, replace the following code

$attach_id = wp_insert_attachment($attachment, $image['path'], $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['path']);

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.

@airani
Copy link
Owner

airani commented Oct 3, 2021

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?

@simo1994
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants