I got error “Specified file failed upload test.” when I try to upload image file using media_handle_sideload(). It took me several hours just to solve this. Haha
My HTML Form
1 2 3 4 |
<form class="row g-3 needs-validation" action="" method="post" enctype="multipart/form-data">< <input type="file" class="form-control" name="filename"> <button type="submit" class="btn btn-primary">Save</button>> </form> |
My PHP
1 |
$media_id = media_handle_sideload($_FILES, 0, 'file description'); |
The error is not really verbose, it should say empty file found rather then failed test. My mistake I forgot to put the index ‘filename’ as in my form.
Solution
1 |
$media_id = media_handle_sideload($_FILES['filename'], 0, 'file description'); |