当前位置:Gxlcms > PHP教程 > {"error":"invalidmultipartformat"}这是什么原因,我已经在HEADER中设置了Content-type:multipart/form-data

{"error":"invalidmultipartformat"}这是什么原因,我已经在HEADER中设置了Content-type:multipart/form-data

时间:2021-07-01 10:21:17 帮助过:108人阅读

用CULR向七牛的API上传文件 每次总是invalid multipart format 但是我已经设置了Content-type:multipart/form-data呀

function uploadFile($localPath, $remoteFileName, $headers=array()){
    global $conf;
    $remoteFileName = trim($remoteFileName,'/');
    $policy =  array('scope' => $conf['bucket'] . ':' . $remoteFileName, 'deadline' => time() + 3600);
    $encodedFlags = encode(json_encode($policy));
    $sign = hash_hmac('sha1', $encodedFlags, $conf['sk'], true);
    $encodedSign = encode($sign);
    $token = $conf['ak'].':'.$encodedSign. ':' . $encodedFlags;
    $fields = array(
        'token' => $token,
        'key'   => $remoteFileName,
        'file' => '@'.$localPath
    );
    $curl = Magecho_HttpClient::get();
    $curl->setOptions(array('gzip'=>false))->setHeader(array(
        'Content-type:multipart/form-data'
    ))->post($conf['host']['up'] ,$fields);
    return $curl->response();
}
function encode($str) {
    $find = array('+', '/');
    $replace = array('-', '_');
    return str_replace($find, $replace, base64_encode($str));
}
$result = uploadFile('pic.jpg',date('Y-m-d-H-i-s').'.jpg');
echo $result;

回复内容:

用CULR向七牛的API上传文件 每次总是invalid multipart format 但是我已经设置了Content-type:multipart/form-data呀

function uploadFile($localPath, $remoteFileName, $headers=array()){
    global $conf;
    $remoteFileName = trim($remoteFileName,'/');
    $policy =  array('scope' => $conf['bucket'] . ':' . $remoteFileName, 'deadline' => time() + 3600);
    $encodedFlags = encode(json_encode($policy));
    $sign = hash_hmac('sha1', $encodedFlags, $conf['sk'], true);
    $encodedSign = encode($sign);
    $token = $conf['ak'].':'.$encodedSign. ':' . $encodedFlags;
    $fields = array(
        'token' => $token,
        'key'   => $remoteFileName,
        'file' => '@'.$localPath
    );
    $curl = Magecho_HttpClient::get();
    $curl->setOptions(array('gzip'=>false))->setHeader(array(
        'Content-type:multipart/form-data'
    ))->post($conf['host']['up'] ,$fields);
    return $curl->response();
}
function encode($str) {
    $find = array('+', '/');
    $replace = array('-', '_');
    return str_replace($find, $replace, base64_encode($str));
}
$result = uploadFile('pic.jpg',date('Y-m-d-H-i-s').'.jpg');
echo $result;

post数据$fileds不需要encode吗?

解决了吗 我也遇到一样的问题

人气教程排行