AVIF’s file permissions bug
-
Hi,
I’ve run into a strange bug when generating thumbnails.
After uploading an image to the WordPress media library, the plugin automatically generates thumbnails—in my case, Retina and AVIF formats.
So, if for images like
image-size1xsize2.png,image-size1xsize2@2.png, andimage-size1xsize2@ 2.png.avif, the file permissions are set correctly (i.e., 00644), but for images likeimage-size1xsize2.png.avif, the file permissions are set incorrectly (i.e., 00660), which results in a 403 Access Denied error on the front end.Why on earth are classic public files suddenly being treated as shared private files?
This is a relatively new issue: it never happened before.
-
Hey @pharmanext! 👋
When generating images, the plugin normally goes through the standard WordPress media process, which automatically sets the correct file permissions.
However, for WebP/AVIF generation, the process is more manual and relies directly on your server’s GD or Imagick library:
- If GD is unavailable or fails, Imagick is used instead
- Imagick does not always automatically apply the correct file permissions
- In some edge cases, this can lead to permission-related issues like the one you encountered
We’ll make sure proper permissions are explicitly set in the next plugin update so this issue no longer happens.
Thanks for your patience and understanding!
add_action('add_attachment', 'fix_meow_all_avif_permissions', 20);
add_filter('wp_generate_attachment_metadata', 'fix_meow_all_avif_permissions_meta', 20, 2);
function fix_meow_all_avif_permissions_meta($metadata, $attachment_id) {
$upload_dir = wp_upload_dir();
$base_dir = $upload_dir['basedir'] . '/';
if (!empty($metadata['file'])) {
$dir_path = dirname($base_dir . $metadata['file']) . '/';
// Check AVIF for the original file
$orig_avif = $base_dir . $metadata['file'] . '.avif';
if (file_exists($orig_avif)) {
@chmod($orig_avif, 0644);
}
// Check AVIF for the created thumbs
if (!empty($metadata['sizes'])) {
foreach ($metadata['sizes'] as $size_info) {
if (!empty($size_info['file'])) {
$thumb_path = $dir_path . $size_info['file'];
// Standard AVIF
if (file_exists($thumb_path . '.avif')) {
@chmod($thumb_path . '.avif', 0644);
}
// Retina AVIF
$ext = pathinfo($size_info['file'], PATHINFO_EXTENSION);
$filename = pathinfo($size_info['file'], PATHINFO_FILENAME);
$retina_avif = $dir_path . $filename . '@2x.' . $ext . '.avif';
if (file_exists($retina_avif)) {
@chmod($retina_avif, 0644);
}
}
}
}
}
return $metadata;
}
function fix_meow_all_avif_permissions($post_id) {
if (wp_attachment_is_image($post_id)) {
$file_path = get_attached_file($post_id);
if ($file_path && file_exists($file_path . '.avif')) {
@chmod($file_path . '.avif', 0644);
}
}
}For anyone else who has encountered this issue, here is some code that sets the necessary permissions for AVIF files after they are generated.
You must be logged in to reply to this topic.