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."?
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."?
Share Improve this question asked May 22, 2013 at 7:40 Pi LoverPi Lover 761 gold badge1 silver badge8 bronze badges3 Answers
Reset to default 1Check 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:
PHP Notice: Undefined index: tmp_name
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/reference/functions/_wp_handle_upload/#source (line #792).