大家都使用过curl进行文件上传 ,我最近做到这个功能,既然遇到了,所以拿出来跟大家分享,大神请无视。
以php5.5为分界线
5.5以下的我们使用老方式
$post = array('name' => 'file', 'file' => '@' . $html_file);
$post['dir'] = 'c'.$id;
$post['type'] = $type;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
//这个兼容低版本的php
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
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://pageupload.sevensme.com/index.php");//上传类
$info= curl_exec($ch);
$result = json_decode($info,true);
curl_close($ch);
5.5以上 ,我们采用的是下面
这样就ok了。