This is my code:
$file_return = wp_handle_upload($_FILES['my-photo'], array('test_form' => FALSE));
if(isset($file_return['error']) || isset($file_return['upload_error_handler'])) {
echo "And so it DIED...";
print_r($file_return);
die;
}
Why is not uploading to the uploads dir and why is it throwing a “Specified file failed upload test.”?
Check the documentation wp_handle_upload
You need to include the file.php before calling
wp_handle_upload
require_once( ABSPATH . 'wp-admin/includes/file.php' )
Check may be same filename already uploaded. I was getting same error because of same filename.
The
wp_handle_upload
tries to read the file data from the lowest dimension of the given array. Like:$file['tmp_name']
. So, if the given info is on a, i.e., secondary layer of the array (like$file['my-photo']['image']
), it will not find the expected index, and fire a notice in your log, like this:And fail the upload test. Check your error log for warnings. Also, check the method’s source for your error message, this might help you a lot: https://developer.wordpress.org/reference/functions/_wp_handle_upload/#source (line #792).