Skip to content

Commit 79b0cd8

Browse files
author
苏青安
committed
增加下载远程文件并保存到本地方法:HttpClient::downloadFile()
1 parent 600de0e commit 79b0cd8

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ So, I’ve compiled these simple and frequently-used methods into this toolkit.
8080

8181
### Network Request Operations
8282

83-
| method | describe |
84-
| :---------------------------- | :----------------------------- |
85-
| HttpClient::sendGetRequest() | Send a GET request using cURL |
86-
| HttpClient::sendPostRequest() | Send a POST request using cURL |
83+
| method | describe |
84+
| :---------------------------- | :------------------------------------------ |
85+
| HttpClient::sendGetRequest() | Send a GET request using cURL |
86+
| HttpClient::sendPostRequest() | Send a POST request using cURL |
87+
| HttpClient::downloadFile() | Download remote files and save them locally |
8788

8889
### Image Operations
8990

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ composer require hejunjie/utils
8585
| :---------------------------- | :----------------------- |
8686
| HttpClient::sendGetRequest() | 使用 cURL 发送 GET 请求 |
8787
| HttpClient::sendPostRequest() | 使用 cURL 发送 POST 请求 |
88+
| HttpClient::downloadFile() | 下载远程文件并保存到本地 |
8889

8990
### 图片操作
9091

src/HttpClient.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,52 @@ public static function sendPostRequest(string $url, array $headers = [], mixed $
9696
'data' => $response
9797
];
9898
}
99+
100+
/**
101+
* 下载远程文件并保存到本地
102+
*
103+
* @param string $url 资源链接
104+
* @param string $savePath 本地存储目录
105+
* @param string|null $filename 自定义文件名(可选)
106+
*
107+
* @return string 返回保存后的绝对路径
108+
* @throws Exception 下载或保存失败时抛出异常
109+
*/
110+
public static function downloadFile(string $url, string $savePath, ?string $filename = null): string
111+
{
112+
// 尝试获取文件内容
113+
$content = @file_get_contents($url);
114+
if ($content === false) {
115+
throw new \Exception("无法下载资源:$url");
116+
}
117+
118+
// 确保保存路径存在
119+
if (!is_dir($savePath)) {
120+
if (!mkdir($savePath, 0777, true) && !is_dir($savePath)) {
121+
throw new \Exception("无法创建目录:$savePath");
122+
}
123+
}
124+
125+
// 提取原始文件后缀
126+
$urlPath = parse_url($url, PHP_URL_PATH);
127+
$extension = pathinfo($urlPath, PATHINFO_EXTENSION);
128+
$extension = $extension ? ".$extension" : '';
129+
130+
// 生成文件名
131+
if (!$filename) {
132+
$filename = uniqid('file_', true) . $extension;
133+
} elseif (!str_ends_with($filename, $extension) && $extension) {
134+
$filename .= $extension;
135+
}
136+
137+
// 组合完整保存路径
138+
$fullPath = rtrim($savePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
139+
140+
// 保存文件
141+
if (file_put_contents($fullPath, $content) === false) {
142+
throw new \Exception("无法保存文件到:$fullPath");
143+
}
144+
145+
return realpath($fullPath) ?: $fullPath;
146+
}
99147
}

0 commit comments

Comments
 (0)