您的位置:首页 > 编程语言 > PHP开发

php 通过url远程下载图片

2019-05-30 18:12 330 查看

php 通过url远程下载图片

//远程下载图片
function get_photo($url,$savePath,$filename='')
{
$imgArr = array('gif','bmp','png','ico','jpg','jepg');

$basePath = 'D:/wwwroot/qhmh/img/';
if(!$url) return false;

if(!$filename) {
$ext=strtolower(explode('.',$url)[3]);
if(!in_array($ext,$imgArr)) return false;
$filename=$this->create_randomstr(32).time().date("dMYHis").'.'.$ext;
}

if(!is_dir($basePath.$savePath)) mkdir($basePath.$savePath, 0777,true);
if(!is_readable($basePath.$savePath)) chmod($basePath.$savePath, 0777,true);

$filenames = $basePath.$savePath.$filename;

ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);

$fp2=@fopen($filenames, "a");
fwrite($fp2,$img);
fclose($fp2);

return $savePath.$filename;
}

/**
* @Title: create_randomstr
* @Description: 获取随机字符串
* @param @param number $lenth  字符长度
*/
function create_randomstr($lenth = 8) {
$str = '';
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i<$lenth;$i++){
$str.=$strPol[rand(0,$max)];
}
return $str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: