2

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."?

3 Answers 3

1

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' )

2
  • I am doing that and this is the error that I am getting: Specified file failed upload test.
    – Pi Lover
    Commented May 22, 2013 at 8:00
  • 1
    your code is working fine. You have used enctype="multipart/form-data" in your form attribute ?
    – Sumit
    Commented May 22, 2013 at 9:13
1

Check may be same filename already uploaded. I was getting same error because of same filename.

1
  • I can't believe and don't still understand why changing the name of the input fixed it. Commented Nov 11, 2021 at 21:33
0

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.org/reference/functions/_wp_handle_upload/#source (line #792).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.