通过wp_handle_upload删除未放入uploads文件夹的图像 [英] Remove an image that was not put in uploads folder via wp_handle_upload

查看:396
本文介绍了通过wp_handle_upload删除未放入uploads文件夹的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图片保存到uploads文件夹,但我正在使用 file_put_contents 而不是 wp_handle_upload - 因为我在base64中获得了一个图像而不是$ _FILES中的文件。

I'm saving an image to uploads folder, but I'm using file_put_contents instead of wp_handle_upload - because I get an image in base64 and not as a file in $_FILES.

图像和某些发布数据会保存/更新,因为它们应该使用此功能:

Image and certain post data are saved/updated as they should be using this functions:


  • wp_insert_attachment

  • wp_update_attachment_metadata

问题是我要删除旧的图像(保存新图像时)。

The problem is when I want to remove an old image (when saving a new one).

wp_delete_attachment 不会删除图片(似乎确实如此)删除db中的东西虽然..)。我认为问题在于不使用wp_handle_upload。 (当我通过上传btn上传图像并使用$ _FILES接收它然后使用wp_handle_upload上传它 - 删除作品)

wp_delete_attachment does not remove the image (it does seem to remove stuff in db though..). I am thinking the problem lies in not using wp_handle_upload. (when I upload an image via upload btn and receive it using $_FILES and then upload it with wp_handle_upload - removing works)

有没有人知道可能是什么在我的情况下删除图像的正确方法?
即使我在base64中有图像,也许我可以使用wp_handle_upload正确保存它?

Does anyone have an idea of what might be the right way to remove an image in my case? Perhaps I can save it properly using wp_handle_upload even if I have an image in base64?

感谢您的任何信息。

编辑:我还尝试使用保存图片wp_upload_bits 和wp_delete_attachment仍然无法正常工作。

I also tried saving image with wp_upload_bits and wp_delete_attachment still did not work.

我检查了另一件事:代码位于 wp-admin / includes / file.php :我看不到一种简单的方法来修改或复制现有的函数并添加一个自定义的函数,它将接受base64图像而不是$ _FILES中的文件。有人可能有base64到$ _FILES的解决方法吗?

Another thing I checked: the code of wp_handle_upload function located in wp-admin/includes/file.php: I don't see an easy way to modify or copy the existing function and add a custom one which would accept base64 image instead of a file as in $_FILES. Someone has a "base64 to $_FILES" workaround perhaps?

推荐答案

正如@KamilP所说,WP在 wp_posts 中插入记录和 wp_postmeta tables。

as @KamilP said, WP inserts records in wp_posts and wp_postmeta tables.

首先你需要将base64图像保存在临时目录中,然后使用它的相对路径和您可以使用 wp_insert_attachment 在数据库中插入记录的其他数据,链接包含适当的示例。
此功能将图像添加到媒体库。

So first you have to save your base64 images in temporary directory, then by using it's relative path and other data you can insert record in database by using wp_insert_attachment, link contains appropriate example. This function will add image to media library.

要生成更多缩略图,您可以使用 wp_generate_attachment_metadata 功能。此函数还将更新wp_postmeta表以及图像和缩略图的所有详细信息。

To generate more thumbnails you can use wp_generate_attachment_metadata function. This function will update wp_postmeta table also with all details of image and thumbnails.

此后你可以使用 wp_delete_attachment 用于从目录和数据库中删除图像的功能。

After this you can use wp_delete_attachment function to delete image from directory and database as well.


  • 使用此链接从base64字符串生成图像。

  • 然后获取mime类型,大小和图像路径

  • 创建一个类似$ _FILES的数组。

  • 然后将其传递给 wp_handle_upload

  • Use function from this link to generate image from base64 string.
  • then get mime type, size and path of image
  • create an array similar to $_FILES's.
  • then pass it to wp_handle_upload

$ _ FILES数组的结构是这样的

$_FILES array's structure is like this

array(5) {
    'name'     => string(8) "file name.extension" // file name with extension
    'type'     => string(0) "" // mime type of file, i.e. image/png
    'tmp_name' => string(0) "" // absolute path of file on disk.
    'error'    => int(2) // 0 for no error
    'size'     => int(0) // size in bytes
  }

你可以创建如上所示的数组所有细节,使用各种文件处理PHP函数来获取大小和mime类型。名称是您要放置的任何名称,tmp_name是存在文件的服务器上的文件路径,在您的文件夹中从base64字符串保存文件的位置。

You can create array like above with all details, use various file handling PHP functions to get size and mime type. Name is whatever you want to put, and tmp_name is a path of file on server where file is exists, in your case location of folder where you save your file from base64 string.

请参阅上面的更新链接以获取将从base64字符串中获取图像的函数。

See updated link above for function which will give you image from base64 string.

这篇关于通过wp_handle_upload删除未放入uploads文件夹的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆