php curl上传图片传参方式

function request($url, $data = array())
{
if($data && class_exists('CURLFile', false))
{
foreach($data as $k=>$v)
{
if($v{0} == '@')
{
$data[$k] = new CURLFile(substr($v, 1));
}
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$url = '/x/upload';
$param = array('img'=>'@'.$_FILES['img']['tmp_name']);
request($url,$param);

参与评论