wordpress无效表单提交

时间:2016-02-24 02:46:43

标签: php wordpress file-upload

我有一个带有上传按钮的表单,当我点击发送按钮我要上传pdf文件,但是var_dump();打印此错误:“表单提交无效。”

php代码:

if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
if ( isset($_POST["submit"])) {

    $uploadedfile = $_FILES['fileToUpload'];
    $upload_overrides = wp_handle_upload($uploadedfile, array(
        'test_form' => false,
        'mimes' => get_allowed_mime_types()
    ));
    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    if ( $movefile ) {
    echo "File is valid, and was successfully uploaded.\n";
    var_dump( $movefile);
    } else {
        echo "Possible file upload attack!\n";
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您是在某处定义$upload_overrides吗?

如果没有,请更改此

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

$movefile = wp_handle_upload( $uploadedfile, array( 'test_form' => false ) );

应该诀窍。

修改

所以,我认为您必须使用media_handle_upload代替wp_handle_upload

require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );

$thumbnail_id = media_handle_upload('fileToUpload', $post_id);

here

中的更多详情