直接上代码
// $source 文件绝对路径 static public function upload($source) { $url = '上传的地址'; $curl = curl_init(); // php5.5以上不准用@ 而用CURLFile类 if (class_exists('\CURLFile')) { curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true); $data = array('file' => new \CURLFile(realpath($source)));//php版本>=5.5 } else { if (defined('CURLOPT_SAFE_UPLOAD')) { curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false); } $data = array('file' => '@' . realpath($source));//php版本<=5.5 } $size = filesize($source); // 详情 请查看 curl_setopt() curl_setopt($curl, CURLOPT_HEADER, false); // 是否带头信息 curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, 1 ); // 设置post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // post 请求数据 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_INFILESIZE, $size); $result = curl_exec($curl); curl_close($curl); return json_decode($result, true); }