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;
}
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;
}