http:
function httpDownload($url, $path = '', $filename = '', $timeout = 60,$type = 0) { if ($url == '') { return false; } //获取远程文件数据 if ($type === 0) { $ch = curl_init(); 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) < 5) { $filename .= $ext; } //防止"/"没有添加 $path = rtrim($path, "/") . "/"; //var_dump($path.$filename);die(); $fp2 = @fopen($path . $filename, 'a'); fwrite($fp2, $file); fclose($fp2); //echo "finish"; return $filename; }
https:
function httpsDownload($url, $path = '', $filename = '', $timeout = 60, $type = 0) { if ($url == '') { return false; } //获取远程文件数据 if ($type === 0) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//最长执行时间 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);//最长等待时间 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false); $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) < 5) { $filename .= $ext; } //防止"/"没有添加 $path = rtrim($path, "/") . "/"; //var_dump($path.$filename);die(); $fp2 = @fopen($path . $filename, 'a'); fwrite($fp2, $file); fclose($fp2); //echo "finish"; return $filename; }