1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php获取远程图片模拟post file上传到指定服务器

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

时间:2022-11-13 16:39:39

相关推荐

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;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。