I've written about similar features before, but I didn't take notes at the time, so I eventually forgot about them. Recently, I've started writing about this again, so I'd like to record it here!

PHP code:
public function downloadFile($url, $path) {
// 判断文件是否存在
if (file_exists($path)) {
// 判断文件大小是否大于10KB
if (filesize($path) > 10240) {
// 文件已经成功下载过
return str_replace(dirname(dirname(__DIR__)), '', $path);
}
}
// echo 123;
// 初始化 cURL
$ch = curl_init($url);
// 设置保存文件路径及文件句柄
$fp = fopen($path, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
// 执行下载
$result = curl_exec($ch);
// 关闭文件句柄和 cURL 资源
fclose($fp);
curl_close($ch);
// 根据下载结果返回对应的值
if ($result !== false) {
return str_replace(dirname(dirname(__DIR__)), '', $path);
} else {
// 处理错误
return $url;
}
}Code Usage:
Add it to your class library, then simply pass the download link and the absolute storage path as arguments; please make the necessary modifications to the code and update it accordingly. The code is provided for reference only.