admin管理员组

文章数量:1122826

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>";

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>";
Share Improve this question asked Jun 13, 2019 at 22:43 IanIan 11 silver badge2 bronze badges 2
  • Which file you have to upload? – Milan Hirpara Commented Jun 14, 2019 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 Commented Jun 14, 2019 at 11:31
Add a comment  | 

1 Answer 1

Reset to default 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>";

本文标签: wphandleupload specified file failed upload test