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 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)
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?
Thanks for any info.
EDIT: I also tried saving image with wp_upload_bits and wp_delete_attachment still did not work.
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?
I actually accomplished this before I saw an answer here, so I'm providing a solution here if someone runs into the same problem. I used the function below (first one) and also using wp_delete_attachment after just to be sure all stuff got removed.
The simple solution is probably to just manually add
_wp_attached_file
meta value to your attachment.Set it to the file you want deleted when the attachment is deleted.
The logic behind this is according to https://core.trac.wordpress.org/browser/tags/4.4/src/wp-includes/post.php#L0:
get_attached_file
returns that value towp_delete_attachment
which sets it as$file
And at the end of
wp_delete_attachment
it callswp_delete_file($file)
So I'm guessing the underlying issue is that, you are never setting a value for the meta key
_wp_attached_file
.The only downside is that I'm not sure what other processes in wp might be using that value. But you might get lucky, perhaps none, and/or it doesn't mess anything up by setting it manually.
as @KamilP said, WP inserts records in
wp_posts
andwp_postmeta
tables.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.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.After this you can use
wp_delete_attachment
function to delete image from directory and database as well.Another solution:
wp_handle_upload
UPDATE :
$_FILES array's structure is like this
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.
See updated link above for function which will give you image from base64 string.