<?php
//远程下载文件
//$url 文件下载地址
//$save_dir 存储路径
//存储名称
function getFile($url, $save_dir = '', $filename = '') {
//创建保存目录
if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
return false;
}
//获取远程文件所采用的方法
$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_SSL_VERIFYPEER, FALSE); // 禁止 cURL 验证对等证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //禁止检查服务器SSL证书
$content = curl_exec($ch);
curl_close($ch);
//文件大小
$size = strlen($content);
//写入文件
$fp2 = @fopen($save_dir . $filename, 'a');
fwrite($fp2, $content);
fclose($fp2);
//销毁
unset($content, $url);
//返回
return array(
'file_name' => $filename,
'save_path' => $save_dir.$filename,
'file_size' => $size
);
}
$url='https://www.51zuso.com/static/index/img/logo.png';
$save_dir = "./";
$filename ="logos.png";
$res = getFile($url, $save_dir, $filename,1);//0 1 都是好使的
var_dump($res);
?>