Php 获取错误“;指定的文件上载测试失败“;从Base64添加文件时解码

Php 获取错误“;指定的文件上载测试失败“;从Base64添加文件时解码,php,wordpress,file-upload,Php,Wordpress,File Upload,我在网上到处搜索,找不到解决办法 我正试图使用wp\u handle\u upload(),但只有在我尝试上载我从base64\u decode()创建的文件时,我才得到“指定文件上载测试失败” 我很确定这不是权限或服务器问题。以下是更多的注意事项: 将WordPress上的文件上载到媒体库时,一切正常 当我直接从$\u文件获取文件时,我的代码甚至可以工作。当我开始创建自己的$file数组时,它停止了工作 我在一个完全不同的服务器上有另一个实时版本,问题也是一样的 我尝试了文件类型的'imag

我在网上到处搜索,找不到解决办法

我正试图使用
wp\u handle\u upload()
,但只有在我尝试上载我从
base64\u decode()创建的文件时,我才得到“指定文件上载测试失败”

我很确定这不是权限或服务器问题。以下是更多的注意事项:

  • 将WordPress上的文件上载到媒体库时,一切正常
  • 当我直接从
    $\u文件
    获取文件时,我的代码甚至可以工作。当我开始创建自己的
    $file
    数组时,它停止了工作
  • 我在一个完全不同的服务器上有另一个实时版本,问题也是一样的
  • 我尝试了文件类型的
    'image/png'
    'image/jpg'
  • 我使用uploads目录作为
    tmp\u名称
    ,但这没有什么区别
  • 临时文件正在成功上载
更多信息:我添加了行
print\r(已上传文件($file['tmp\u name'));死亡
到/wp admin/includes/file.php中的第822行。我相信这是因为这是错误的,错误被抛出

    $upload_dir       = wp_upload_dir();
    $upload_path      = get_stylesheet_directory().'/tmp-images/';
    $img = $_POST['main_image_0'];
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $decoded          = base64_decode($img) ;
    $filename         = $_POST['file_name'];
    $hashed_filename  = md5( $filename . microtime() ) . '_' . $filename;

    // @new
    if (!is_dir($upload_path)) {
        // dir doesn't exist, make it
        mkdir('upload/promotions/' . $month);
    }
    
    $image_upload     = file_put_contents( $upload_path . $hashed_filename, $decoded );

    //HANDLE UPLOADED FILE
    if( !function_exists( 'wp_handle_sideload' ) ) {
      require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }

    // Without that I'm getting a debug error!?
    if( !function_exists( 'wp_get_current_user' ) ) {
      require_once( ABSPATH . 'wp-includes/pluggable.php' );
    }

    // @new
    $file             = array();
    $file['error']    = 0;
    $file['tmp_name'] = $upload_path . $hashed_filename;
    $file['name']     = $filename;
    $file['type']     = 'image/jpeg';
    $file['size']     = filesize( $upload_path . $hashed_filename );        
    
    require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );

    $file_return = wp_handle_upload( $file, array('test_form' => false ) );
    print_r($file);print_r($file_return);die;
这是回报

Array
(
    [error] => 0
    [tmp_name] => /var/www/example/wp-content/themes/mytheme/tmp-images/eea84bdc7b9b79a10898425c79f62fc2_20130202_173509.jpg
    [name] => 20130202_173509.jpg
    [type] => image/jpeg
    [size] => 273444
)
Array
(
    [error] => Specified file failed upload test.
)

我找到了原因。这是由于使用了
is\u uploaded\u file
的功能造成的。所以一切都按它应该的方式进行。我只是没有意识到
wp\u handle\u upload()
只是为了这个目的。在调用函数之前,我使用了base64并对其进行了转换,这意味着它不再处理post请求中的文件


为了解决我的问题,我使用了
wp\u upload\u bits
()。

我找到了原因。这是由于使用了
is\u uploaded\u file
的功能造成的。所以一切都按它应该的方式进行。我只是没有意识到
wp\u handle\u upload()
只是为了这个目的。在调用函数之前,我使用了base64并对其进行了转换,这意味着它不再处理post请求中的文件

为了解决我的问题,我使用了
wp\u upload\u bits
()