php获取远程图片模拟post,file上传到指定服务器

1.获取远程图片

/**

$path保存图片的地址

$url要获取的远程图片地址

**/

function getimg($path,$url){

$aext = explode('.', $url);
$ext = end($aext);
$name = $path.'/'. time() . '.' . $ext;
$source=file_get_contents($url);
file_put_contents($name,$source);
return $name;

}

2.上传图片

/**

$posturl上传图片的地址

$path本地图片所在的地址

**/

function postimg($posturl,$path){

$obj = new CurlFile($path);
$obj->setMimeType("application/octet-stream");//必须指定文件类型,否则会默认为application/octet-stream,二进制流文件</span>
$post['Filedata'] = $obj;
$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, $posturl);//上传类
$info= curl_exec($ch);
curl_close($ch);
return $info;

}