The move_uploaded_file
function is a PHP function:
http://php.net/manual/en/function.move-uploaded-file.php
One important thing to note about it from that page:
This function checks to ensure that the file designated by filename is
a valid upload file (meaning that it was uploaded via PHP’s HTTP POST
upload mechanism). If the file is valid, it will be moved to the
filename given by destination.
You’re not uploading a file here, you’re downloading it from a URL, saving it locally, and then attempting to use wp_handle_upload
(which uses move_uploaded_file
) to handle it. This fails because it’s not actually an uploaded file.
What you’re actually trying to do is called a “sideload”, where you get a file from URL and load it in directly. WordPress has a function for this specific case, called wp_handle_sideload
. For the specific case of images, WordPress has another function called media_sideload_image
that does much the same thing but also handles all the image processing.
If you really are wanting to upload files and not sideload them from a URL (this could be test code you’re trying to do), then you need to code up a file upload form, get the contents of the $_FILES[0]
parameter, and pass that to wp_handle_upload
. If you’re trying specifically to handle uploaded images or other items for the media library, use media_handle_upload
instead. Uploads through these function have to be real, not faked.