admin管理员组

文章数量:1287892

I couldn't figure out how wordpress handles media library.

  • I copied the media files from one vm to another using gcloud compute scp...

  • The files are successfully copied to the wp-content/uploads/ folder of the destination.

  • I updated wp_postmeta and wp_posts and verified that the image urls are updated as intended. However, when I access the images urls, it returns 404 error.

  • I tried regenerating thumbnail and have errors. The fullsize image file cannot be found in your uploads directory at .... They are not accessible via browser too. However, the files are there in the wp-content/uploads/ folder and the urls are correct.

  1. Do all images need to go through the upload process in wordpress?
  2. Images deleted from the server still have reference in the media library albeit the images are not found. Is this the intended behavior?

I couldn't figure out how wordpress handles media library.

  • I copied the media files from one vm to another using gcloud compute scp...

  • The files are successfully copied to the wp-content/uploads/ folder of the destination.

  • I updated wp_postmeta and wp_posts and verified that the image urls are updated as intended. However, when I access the images urls, it returns 404 error.

  • I tried regenerating thumbnail and have errors. The fullsize image file cannot be found in your uploads directory at .... They are not accessible via browser too. However, the files are there in the wp-content/uploads/ folder and the urls are correct.

  1. Do all images need to go through the upload process in wordpress?
  2. Images deleted from the server still have reference in the media library albeit the images are not found. Is this the intended behavior?
Share Improve this question asked Sep 14, 2021 at 14:08 FlyingPenguinFlyingPenguin 1134 bronze badges 1
  • "They are not accessible via browser too" sounds like you copied them to the wrong location and/or it is not accessible. Even when the files don't appear in WP media library, you should be able to get them directly. – kero Commented Sep 14, 2021 at 16:52
Add a comment  | 

1 Answer 1

Reset to default 1
  1. Yes, pretty much.
  2. Yes.

When you upload an image to WordPress a few things happen. These might not be in the precise order, I'm going off memory, but it shouldn't change much:

  1. It makes sure the image has a unique filename and puts it in the wp-content/uploads directory.
  2. It creates an attachment post for the image.
  3. It saves the file path of the original image as post meta.
  4. It generates metadata for the image, including dimensions etc. and saves it as post meta.
  5. It uses registered WordPress image sizes to crate cropped and resized versions of the image which are saved in the uploads directory.
  6. It saves the file paths and dimensions of the resized versions of the image to post meta.

When an attachment is deleted from the media library, the original and resized versions of the image are deleted from the server.

What's important to know in your situation is that when an image is deleted from the server, absolutely nothing happens in WordPress. The file will just be missing when WordPress attempts to find it.

WordPress also never scans the uploads directory and lists files in the media library. It's not a file explorer, and only lists attachments from the database. It is build so all media management is done in WordPress. If you try to manage media directly on the server you will get unexpected results in the media library.

There's a few functions you should be aware of for what you're trying to do:

  • media_handle_upload() - Saves a file submitted from a POST request and create an attachment post for it.
  • media_handle_sideload() - Similar to media_handle_upload(), but you can use it for images that are already on the server somewhere.
  • update_attached_file() - If you have the path to a file, and want to update an existing attachment to use it, you can use this function.
  • wp_generate_attachment_metadata() - Saves all the meta data for the attachment, and generates the resized and cropped versions. You would want to run this after using update_attached_file().

There's more to it, but that's an overview of what needs to happen for media in WordPress.

本文标签: uploadsHow does wordpress handle media files