php7使用curl上传文件返回错误码26;代码如下:
$size = filesize($path);//文件大小
$ch = curl_init($url);
$cfile = curl_file_create($path);
if (is_null($post_data)) {
$data = array('media' => $cfile);
}else{
$data = array('media' => $cfile,'description'=>$post_data);
}
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_INFILESIZE,$size);
curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$info = curl_exec($ch);
if (curl_errno($ch)) {
var_dump(curl_errno($ch));
return FALSE;
}
curl_close($ch); // 关闭CURL会话
return $info; // 返回数据
我查看手册后使用过CURLFile函数,但返回的错误依然是26错误码,或者是缺少多媒体问题等
$cfile = new \CURLFile($path,$upload_type,$upload_name);
php版本
<=5.4 curl上传文件只支持@语法
= 5.5 支持@语法和CURLFile类
大于=5.6 只支持CURLFile类// 兼容性写法参考示例
$curl = curl_init();
if (class_exists('\CURLFile')) {// 这里用特性检测判断php版本
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
$data = array('file' => new \CURLFile(realpath($source)));//>=5.5
} else {
if (defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
}
$data = array('file' => '@' . realpath($source));//<=5.5
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1 );
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
$result = curl_exec($curl);
$error = curl_error($curl);