PHP版本高於5.5時,curl檔案上傳必須使用CurlFile物件
阿新 • • 發佈:2019-01-09
上傳類測試用例
public function testUpload01(){ $file = __DIR__.'\assets\test.jpg'; //var_dump($file); //$post['file'] = '@'.$file; $obj = new CurlFile($file); <span style="color:#ff0000;">$obj->setMimeType("image/jpeg");//必須指定檔案型別,否則會預設為application/octet-stream,二進位制流檔案</span> $post['file'] = $obj; $post['abc'] = "abc"; var_dump($post); $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, false); //啟用時會發送一個常規的POST請求,型別為:application/x-www-form-urlencoded,就像表單提交的一樣。 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_BINARYTRANSFER,true); curl_setopt($ch, CURLOPT_POSTFIELDS,$post); curl_setopt($ch, CURLOPT_URL, "http://localhost/fs/upload.php");//上傳類 $info= curl_exec($ch); curl_close($ch); var_dump($info); file_put_contents('./1.html',$info); $res=json_decode($info,true); //var_dump($res); }