filesize(): stat failed for IMAGE on external storage like WPEngine LargeFS
- [x] I've read and understood the contribution guidelines.
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Please give us a description of what happened.
Similar symptom as https://github.com/Yoast/wordpress-seo/issues/10002 but different underlying cause.
We use WPEngine's LargeFS to offload, store, and dynamically serve all site images from S3. It works seamlessly in most cases. But the images are no longer actually stored on WPEngine and so any file operations like filesize() which relies on physical paths derived from web URLs will fail. This will also be a problem for any similar image offloading mechanism in which the files are not in the same relative server path as their URL.
Please describe what you expected to happen and why.
It seems like filesize() should be completely avoided on something as dynamic as images which can be loaded such a wide variety of ways and from such a wide variety of storage solutions.
How can we reproduce this behavior?
Use LargeFS on WPEngine; OR (in theory)
- Add an image to WP and make sure it's referenced in theme or post.
- Move that image file to a different storage location
- Create a WP Rewrite to change requests for it's original URL to serve from the new URL
(Actually through step 2 should be sufficient to generate the issue, but step 3 is of course required if you want the image to still load on the page).
Technical info
Here's an example sanitized stack:
#0 {closure}(2, filesize(): stat failed for /wp-content/uploads/2020/04/Logo.png,
/wp-content/plugins/wordpress-seo/inc/class-wpseo-image-utils.php, 247,
Array ([image] => Array ([width] => 233,[height] => 84,[url] =>
https://SITENAME.com/wp-content/uploads/2020/04/Logo.png,[path] =>
/wp-content/uploads/2020/04/Logo.png,[size] => full,[id] => 264,[alt] =>
Logo,[pixels] => 19572,[type] => image/png)))
#1 filesize(/wp-content/uploads/2020/04/Logo.png)
called at [/wp-content/plugins/wordpress-seo/inc/class-wpseo-image-utils.php:247]
#2 WPSEO_Image_Utils::get_file_size(Array ([width] => 233,[height] => 84,
[url] => https://SITENAME.com/wp-content/uploads/2020/04/Logo.png,[path] =>
/wp-content/uploads/2020/04/Logo.png,[size] => full,[id] => 264,[alt] =>
Logo,[pixels] => 19572,[type] => image/png))
called at [/wp-content/plugins/wordpress-seo/inc/class-wpseo-image-utils.php:141]
#3 WPSEO_Image_Utils::has_usable_file_size(Array ([width] => 233,[height] => 84,
[url] => https://SITENAME.com/wp-content/uploads/2020/04/Logo.png,[path] =>
/wp-content/uploads/2020/04/Logo.png,[size] => full,[id] => 264,[alt] =>
Logo,[pixels] => 19572,[type] => image/png))
called at [/wp-content/plugins/wordpress-seo/inc/class-wpseo-image-utils.php:313]
#4 WPSEO_Image_Utils::filter_usable_file_size(Array ([0] => Array ([width] => 233,[height] => 84,
[url] => https://SITENAME.com/wp-content/uploads/2020/04/Logo.png,[path] =>
/wp-content/uploads/2020/04/Logo.png,[size] => full,[id] => 264,[alt] =>
Logo,[pixels] => 19572,[type] => image/png)))
called at [/wp-content/plugins/wordpress-seo/src/helpers/image-helper.php:257]
#5 Yoast\WP\SEO\Helpers\Image_Helper->get_best_attachment_variation(264)
called at [/wp-content/plugins/wordpress-seo/src/helpers/open-graph/image-helper.php:110]
#6 Yoast\WP\SEO\Helpers\Open_Graph\Image_Helper->get_image_by_id(264)
called at [/wp-content/plugins/wordpress-seo/src/values/open-graph/images.php:57]
#7 Yoast\WP\SEO\Values\Open_Graph\Images->add_image_by_id(264)
called at [/wp-content/plugins/wordpress-seo/src/generators/open-graph-image-generator.php:160]
#8 Yoast\WP\SEO\Generators\Open_Graph_Image_Generator-
>add_from_default(Yoast\WP\SEO\Values\Open_Graph\Images Object ([
Used versions
- WordPress version: 5.4.2
- Yoast SEO version: 14.3
Didn't reproduce this, but I can see it happening. There is no check if the path to the image is actually an absolute reachable path.
https://github.com/Yoast/wordpress-seo/blob/80ef39572234a93fcf762e626495b85e5c0f6d41/inc/class-wpseo-image-utils.php#L245-L254
To add another use case of this error. We are using the WP Offload Media Lite plugin which replaces the path with a URL, which filesize will fail on.
PHP Warning filesize(): stat failed for /var/www/html/wp-content/uploads/s3ams3://my-theme-live-media/2020/08/28095506/image__79236__21844.1598283769.1280.1280.jpg
[internal] filesize
/var/www/html/wp-content/plugins/wordpress-seo/inc/class-wpseo-image-utils.php:253 WPSEO_Image_Utils::get_file_size
/var/www/html/wp-content/plugins/wordpress-seo/inc/class-wpseo-image-utils.php:147 WPSEO_Image_Utils::has_usable_file_size
/var/www/html/wp-content/plugins/wordpress-seo/inc/class-wpseo-image-utils.php:319 WPSEO_Image_Utils::filter_usable_file_size
/var/www/html/wp-content/plugins/wordpress-seo/src/helpers/image-helper.php:257 Yoast\WP\SEO\Helpers\Image_Helper::get_best_attachment_variation
/var/www/html/wp-content/plugins/wordpress-seo/src/helpers/open-graph/image-helper.php:110 Yoast\WP\SEO\Helpers\Open_Graph\Image_Helper::get_image_by_id
/var/www/html/wp-content/plugins/wordpress-seo/src/values/open-graph/images.php:57 Yoast\WP\SEO\Values\Open_Graph\Images::add_image_by_id
/var/www/html/wp-content/plugins/wordpress-seo/src/generators/open-graph-image-generator.php:127 Yoast\WP\SEO\Generators\Open_Graph_Image_Generator::add_from_indexable
/var/www/html/wp-content/plugins/wordpress-seo/src/generators/open-graph-image-generator.php:100 Yoast\WP\SEO\Generators\Open_Graph_Image_Generator::generate
/var/www/html/wp-content/plugins/wordpress-seo/src/presentations/indexable-presentation.php:402 Yoast\WP\SEO\Presentations\Indexable_Presentation::generate_open_graph_images
/var/www/html/wp-content/plugins/wordpress-seo/src/presentations/indexable-post-type-presentation.php:198 Yoast\WP\SEO\Presentations\Indexable_Post_Type_Presentation::generate_open_graph_images
/var/www/html/wp-content/plugins/wordpress-seo/src/presentations/abstract-presentation.php:69 Yoast\WP\SEO\Presentations\Abstract_Presentation::__get
/var/www/html/wp-content/plugins/wordpress-seo/src/presenters/open-graph/image-presenter.php:67 Yoast\WP\SEO\Presenters\Open_Graph\Image_Presenter::get
/var/www/html/wp-content/plugins/wordpress-seo/src/presenters/open-graph/image-presenter.php:35 Yoast\WP\SEO\Presenters\Open_Graph\Image_Presenter::present
/var/www/html/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:267 Yoast\WP\SEO\Integrations\Front_End_Integration::present_head
/var/www/html/wp-includes/class-wp-hook.php:287 WP_Hook::apply_filters
/var/www/html/wp-includes/class-wp-hook.php:311 WP_Hook::do_action
/var/www/html/wp-includes/plugin.php:478 do_action
/var/www/html/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:242 Yoast\WP\SEO\Integrations\Front_End_Integration::call_wpseo_head
/var/www/html/wp-includes/class-wp-hook.php:287 WP_Hook::apply_filters
/var/www/html/wp-includes/class-wp-hook.php:311 WP_Hook::do_action
/var/www/html/wp-includes/plugin.php:478 do_action
/var/www/html/wp-includes/general-template.php:3006 wp_head
/var/www/html/wp-content/themes/my-theme-gb/header.php:148 require_once
/var/www/html/wp-includes/template.php:730 load_template
/var/www/html/wp-includes/template.php:676 locate_template
/var/www/html/wp-includes/general-template.php:48 get_header
/var/www/html/wp-content/themes/my-theme-gb/product.php:15 include
/var/www/html/wp-includes/template-loader.php:106 require_once
/var/www/html/wp-blog-header.php:19 require
/var/www/html/index.php:17 [main]
This is an issue with the S3 plugin as well. CC @rmccue @joehoyle
More detail - #16119
It is not a full fix for this issue, but this filter ( https://github.com/Yoast/wordpress-seo/pull/16177 ) might be useful and would allow other plugins to patch the issue.
We are using Pantheon which has their own storage solution and what not. It is not crashing per se. Instead we get HUGE load times (tracked via NewRelic, please see screenshot here) that are causing quite some problems.
Any way we can pull the hotfix to try and test? Referring to #16177 .
@Uriel1339 Hey, this is a completely untested piece of code, but it is a start. Mock code.
add_filter('wpseo_image_get_data', function( $data, $attachment_id ) {
$meta_key = '_attachment_metadata_sizes';
$filesize = (array) get_post_meta( $attachment_id, $meta_key, true );
$size = $data['size'];
if ( isset( $filesize[ $size ] ) ) {
$data['filesize'] = (int) $filesize[ $size ];
return $data;
}
$path = $data['path'];
$filesize[ $size ] = @filesize( $path );
update_post_meta( $attachment_id, $meta_key, $filesize );
$data['filesize'] = (int) $filesize[ $size ];
return $data;
}, 10, 2 );
Worth noting we using S3 on pantheon.
Good luck
There is a WordPress core issue at #49412.
This issue can be tracked upstream. I am going to cc @jdevalk and @SergeyBiryukov as they have shown interest on this matter in the past.
@Uriel1339 Hey, this is a completely untested piece of code, but it is a start. Mock code.
add_filter('wpseo_image_get_data', function( $data, $attachment_id ) { ... }, 10, 2 );
FYI: The filter was renamed to wpseo_image_data in #16247.
In WordPress core there is now, a function called wp_filesize. See https://github.com/WordPress/wordpress-develop/commit/7fc225aa87c80680df01a0a82e6bb94b7773a34c
I strongly recommend either using that, or the file size stored in post meta.
Please inform the customer of conversation # 896814 when this conversation has been closed.
My Yoast paid is coming up for renewal and I can't believe this still hasn't been fixed. Thank you @spacedmonkey for the xref to #16925