從遠端網站採集圖片時,保持原有目錄結構儲存至本地,例如:
遠端:https://example.com/images/2024/01/photo.jpg 本地:./images/2024/01/photo.jpg
<?php
/**
* 採集圖片並按原路徑儲存
* @param string $domain 來源域名 (含協議,如 https://example.com)
* @param string $urlPath 圖片路徑 (如 /images/2024/01/photo.jpg)
* @param string $localRoot 本地儲存根目錄
* @return array ['success'=>bool, 'path'=>string, 'error'=>string]
*/
function fetchImageKeepPath(string $domain, string $urlPath, string $localRoot = '.'): array {
// 1. 解析路徑資訊
$pathInfo = pathinfo($urlPath);
$dirName = $pathInfo['dirname'] ?? '';
$baseName = $pathInfo['basename'] ?? '';
if (!$baseName) {
return ['success' => false, 'error' => '無效的圖片路徑'];
}
// 2. 建立本地目錄 (遞迴建立、權限 0755)
$localDir = rtrim($localRoot, '/') . '/' . ltrim($dirName, '/');
if (!is_dir($localDir)) {
if (!@mkdir($localDir, 0755, true)) {
return ['success' => false, 'error' => "無法建立目錄: $localDir"];
}
// 可選:放置 index.html 防止目錄列表
@file_put_contents($localDir . '/index.html', '');
}
// 3. 下載圖片
$remoteUrl = rtrim($domain, '/') . '/' . ltrim($urlPath, '/');
$localFile = $localDir . '/' . $baseName;
// 使用 stream context 設定 timeout、user-agent
$context = stream_context_create([
'http' => [
'timeout' => 30,
'user_agent' => 'Mozilla/5.0 (compatible; PHP Image Fetcher)',
'follow_location' => 1,
'max_redirects' => 3,
],
'ssl' => [
'verify_peer' => false, // 採集時常關閉驗證,生產環境建議開啟
'verify_peer_name' => false,
]
]);
$content = @file_get_contents($remoteUrl, false, $context);
if ($content === false) {
return ['success' => false, 'error' => "下載失敗: $remoteUrl"];
}
// 4. 儲存檔案
$bytes = @file_put_contents($localFile, $content);
if ($bytes === false) {
return ['success' => false, 'error' => "寫入失敗: $localFile"];
}
// 5. 驗證圖片完整性 (可選)
$imgInfo = @getimagesize($localFile);
if (!$imgInfo) {
@unlink($localFile);
return ['success' => false, 'error' => '下載內容非有效圖片'];
}
return [
'success' => true,
'path' => $localFile,
'url' => $remoteUrl,
'size' => $bytes,
'mime' => $imgInfo['mime'] ?? 'unknown',
'width' => $imgInfo[0] ?? 0,
'height' => $imgInfo[1] ?? 0,
];
}
// 使用範例
$domain = 'https://www.jb51.net';
$urlPath = '/newskin/images/v4/logo.jpg';
$result = fetchImageKeepPath($domain, $urlPath, __DIR__ . '/collected');
if ($result['success']) {
echo "成功儲存: {$result['path']} ({$result['width']}x{$result['height']}, {$result['mime']})";
echo "<br><img src='{$result['path']}' alt='採集圖片'>";
} else {
echo "失敗: {$result['error']}";
}
?>
<?php
class ImageCollector {
private $domain;
private $localRoot;
private $results = [];
public function __construct(string $domain, string $localRoot) {
$this->domain = rtrim($domain, '/');
$this->localRoot = rtrim($localRoot, '/');
}
public function collect(array $urlPaths): array {
foreach ($urlPaths as $path) {
$result = $this->fetchOne($path);
$this->results[] = $result;
// 禮貌延遲,避免被封 IP
usleep(200000); // 0.2 秒
}
return $this->results;
}
private function fetchOne(string $urlPath): array {
return fetchImageKeepPath($this->domain, $urlPath, $this->localRoot);
}
public function getReport(): string {
$ok = array_filter($this->results, fn($r) => $r['success']);
$fail = array_filter($this->results, fn($r) => !$r['success']);
return sprintf("成功: %d, 失敗: %d", count($ok), count($fail));
}
}
// 使用
$collector = new ImageCollector('https://example.com', '/var/www/images');
$urls = [
'/img/a.jpg',
'/img/b.png',
'/assets/2024/logo.svg',
];
$collector->collect($urls);
echo $collector->getReport();
?>
| 技術點 | 實作細節 |
|---|---|
| 路徑解析 | pathinfo() 取得 dirname/basename/extension |
| 目錄建立 | mkdir($path, 0755, true) 遞迴建立、權限控制 |
| 下載超時 | stream_context 設定 timeout、user-agent、redirect 限制 |
| SSL 驗證 | 採集時常關閉 verify_peer,生產建議開啟並配置 CA 憑證 |
| 完整性驗證 | getimagesize() 確認為有效圖片,無效則刪除 |
| 防重複下載 | 檢查 file_exists() 或資料庫記錄 URL 指紋 (MD5) |
| 並發控制 | 生產環境建議使用佇列 + Worker (Redis/RabbitMQ + Supervisor) |
robots.txt 與網站服務條款。