wp_handle_upload error:”Specified file failed upload test.”
-
Hello,
I am writing a membership area and want users to be able to upload images via forms. I am using the Code Snippets plugin for programming the interface.
For testing purposes I have created a small form and file-handling snippet with following code:include ‘wp-admin/includes/file.php’;
function to_upload_this(){
if(!empty($_FILES[“upload_image”][“name”])){
$fileType = pathinfo($_FILES[“upload_image”][“name”],PATHINFO_EXTENSION);
$allowTypes = array(‘jpg’, ‘png’, ‘jpeg’, ‘svg’);
if(in_array($fileType, $allowTypes)){
//Upload file to the server
$overrides = array( ‘test_form’ => false);
$file = $_FILES[“upload_image”];
$upload = wp_handle_upload($file, $overrides);
if ( is_wp_error( $upload ) ) {
// There was an error uploading the image.
echo “an error”;
print_r($upload);
} else {
// The image was uploaded successfully!
echo “no error”;
print_r($upload);
}
echo ‘Path is allowed.’;}else{
echo ‘Sorry, only JPG, JPEG, PNG & SVG files are allowed to upload.’;
}
}
?>
<form method=”post” enctype=”multipart/form-data”>
<input type=’file’ name=”upload_image”><input type=”submit” name=”submit” value=”Submit”>
</form>
<?phpreturn;
}add_shortcode( ‘tests’,’to_upload_this’ );
The website uses php 7.3.25 and wordpress 5.3.1 and Strato is the host. And when I display phpinfo() it states that “file_uploads” is “on”.
It would be really nice to get some help fixing this and if you need any further information please ask.
The topic ‘wp_handle_upload error:”Specified file failed upload test.”’ is closed to new replies.