xml - rpc wp.上传工作正常,但使图像损坏


XML-RPC wp.upload working fine But make Image corrupted

我尝试了很多方法使用xml-rpc上传图像到wordpress,并获得了文件名,路径和文件类型数组的完美响应。如果我在wordpress中查看图像,它会生成一个0字节的损坏图像文件。

我已经做了一个类来操作所有查询,如创建post/编辑post/删除post等。所有的工作文件只是wp。上传文件不能正常工作

这是我的图片上传功能。

function upload_pic($url, $pic, $type='image/jpg')
{
    $fs = filesize($url);
    $file = fopen($url, 'rb');
    $filedata = fread($file, $fs);
    fclose($file);
    $content = array(
        'name'  => $pic,
        'type'  => $type,
        'bits'  => new IXR_Base64($filedata), 
        'overwrite' => false 
    );
    $params = array(1,$this->UserName,$this->PassWord,$content,true);
    return $this->send_request('wp.uploadFile',$params);
}

我得到以下响应

   Array
   (
       [id] => 190
       [file] => P_1364799102.jpg
       [url] => http://localhost/wordpress/wp-content/uploads/2013/04/P_13647991025.jpg
       [type] => image/jpg
   )

响应看起来很好,但是静止图像文件被损坏了0字节。

请帮我一下。我也试过metaWeblog。

我已经找到了一个修复程序,它现在工作正常。

function upload_pic($postid, $myFile, $name, $type='image/jpeg')
{
    $rpcurl = $this->XMLRPCURL;;
    $username = $this->UserName;
    $password = $this->PassWord;
    $file=file_get_contents($myFile);
    $filetype = $type;
    $filename = $name;
    xmlrpc_set_type($file,'base64'); // <-- required!
    $params = array($postid,$username,$password,array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
    $request = xmlrpc_encode_request('wp.uploadFile',$params);
    $result = xmlrpc_decode($this->go($request,$rpcurl));
    return $result;
}

谢谢