0

The code I am using is practically the same as the codex. The $u and $m return true. Just the $error gives "specified file failed upload test". Thanks for any help you can give.

FORM:

PHP

    if ($uploadedfile = $_FILES['amfile']) {$u = "Uploaded file set";} else  {$u = "Uploaded file NOT set";} 

    $upload_overrides = array( 'test_form' => false );

    if ($movefile = wp_handle_upload( $uploadedfile, $upload_overrides )) {$m = "Movefile file set";} else  {$m = "Movefile file NOT set";}  ;

    if ( $movefile && !isset( $movefile['error'] ) ) {
        $error = "File is valid, and was successfully uploaded.\n";
        var_dump( $movefile );
    } else {

        $error = $movefile['error'];
    }

    echo $u . "<br>";
    echo $m . "<br>";
    echo $error . "<br>";
  • Which file you have to upload? – Milan Hirpara Jun 14 at 6:31
  • It's a pdf, doc or docx file. Loaded in a form and given like this: FORM: <input type="file" name="amfile" size="40" class="wpcf7-form-control wpcf7-file" id="amfile" accept=".pdf,.doc,.docx" aria-invalid="false"> – Ian Jun 14 at 11:31
0

Please add below code in your theme functions.php file :

add_filter( 'wp_check_filetype_and_ext', 'file_and_ext_allow_for_user', 10, 4 );
function file_and_ext_allow_for_user( $types, $file, $filename, $mimes )
{
    if( false !== strpos( $filename, '.doc' ) ) {
        $types['ext'] = 'doc';
        $types['type'] = 'application/msword';
    } else if( false !== strpos( $filename, '.pdf' ) ) {
        $types['ext'] = 'pdf';
        $types['type'] = 'application/pdf';
    } else if( false !== strpos( $filename, '.docx' ) ) {
        $types['ext'] = 'docx';
        $types['type'] = 'application/docx';
    }
    return $types;
}

File Upload Code

if ( ! function_exists( 'wp_handle_upload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

$uploadedfile = $_FILES['amfile'];

if( $uploadedfile ){

    $u = "Uploaded file set";

    $upload_overrides = array( 'test_form' => false );

    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides ); 

    if ( $movefile && !isset( $movefile['error'] ) ) {

        $m = "Movefile file set";

        echo "File is valid, and was successfully uploaded.\n";
        var_dump( $movefile);

    } else {
        $m = "Movefile file NOT set";
        /**
         * Error generated by _wp_handle_upload()
         * @see _wp_handle_upload() in wp-admin/includes/file.php
         */
        print_r($movefile);
        $error = $movefile['error'];
    }


} else  {
    $u = "Uploaded file NOT set";
} 

echo $u . "<br>";
echo $m . "<br>";
echo $error . "<br>";
  • I added the above Milan. I am assuming I have to amend my code somehow to access the $types[]. – Ian Jun 14 at 13:43
  • No need to change in your code just put above code in functions.php file. – Milan Hirpara Jun 16 at 15:07
  • Hi and thanks for the help again. Yes, I did that and I still get the same outcome. – Ian Jun 17 at 0:19
  • @lan please try above file upload code. – Milan Hirpara Jun 17 at 4:45
  • Many thanks for your input Milan. However the results are much the same. $u = Uploaded file set $m = Movefile file NOT set $error = Specified file failed upload test. Could it be to do with priorities set. I have this running from: add_action( 'wpcf7_mail_sent', 'action_wpcf7_mail_sent', 20, 1 ); – Ian Jun 18 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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