PHP版本高于5.5时,curl文件上传必须使用CurlFile对象

来源:互联网 发布:直接引用vue.js没反应 编辑:程序博客网 时间:2024/05/29 19:53
上传类测试用例微笑
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);}

0 0