WP_HANDLE_UPLOAD指定文件上载测试失败

时间:2019-06-13 作者:Ian

我使用的代码实际上与codex相同。$u和$m返回true。只有$错误给出了“指定文件上传测试失败”。谢谢你的帮助。

形式:

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

1 个回复
SO网友:Milan Hirpara

请在主题函数中添加以下代码。php文件:

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

相关推荐

Custom CSS In Uploads Folder

我想知道上传文件夹中是否有自定义CSS文件。我从以前的开发人员那里继承了一个自定义主题,前端的大量样式都是从这个样式表派生出来的,它同样不在主题或插件文件夹中,而是wp-content/uploads 文件夹我正在努力找出它是如何推导出来的,但它确实存在。这是否被视为不良做法,或者是否有这样的用例?我相信之前的开发人员提到了Visual Composer或JS Composer以及编译更少的文件。。。但我对此不确定,他已经失去了联系,我想知道为什么我们要以这种方式使用样式表,因此这个问题是关于最佳实践的,