<?php
header("Content-Type:text/html;charset=UTF-8");
function getFile($url='',$filename='',$path='',$type=0){
//获取远程文件数据
if($type===0){
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);//最长执行时间
curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);//最长等待时间
$file=curl_exec($ch);
curl_close($ch);
}
if($type===1){
ob_start();
readfile($url);
$file=ob_get_contents();
ob_end_clean();
}
if($type===2){
$file=file_get_contents($url);
}
//判断下载的数据 是否为空 下载超时问题
if(empty($file)){
throw new \Exception("下载错误,无法获取下载文件!");
}
//没有指定路径则默认当前路径
if($path===''){
$path="./";
}
//如果命名为空
if($filename===""){
$filename=md5($file);
}
//获取后缀名
$ext=substr($url, strrpos($url, '.'));
if($ext && strlen($ext)<9){
$filename.=$ext;
}
//防止"/"没有添加
$path=rtrim($path,"/")."/";
// var_dump($path.$filename);die();
$fp2=@fopen($path.$filename,'a');
fwrite($fp2,$file);
fclose($fp2);
return $filename;
}
$arr = [];
$arr['name1'] = 'http://47.52.115.175/index.html';
$arr['name2'] = 'http://47.52.115.175/index.html';
$arr['name3'] = 'http://47.52.115.175/index.html';
foreach ($arr as $key=>$val){
getFile($val,$key);
}
?>