php如何打包下载远程图片

现在页面上有多张图片(来自 OSS),如何根据这些图片的 url 来一次性将图片全部下载到本地?

阅读 4.9k
2 个回答

如果你想在 浏览器端做这个功能就需要借助 js 实现
如果你想在 服务端实现这个功能,可以一张一张获取,然后打包。

From : https://stackoverflow.com/que...

$image1 = "http://cdn.screenrant.com/wp-content/uploads/Darth-Vader-voiced-by-Arnold-Schwarzenegger.jpg";
$image2 = "http://cdn.screenrant.com/wp-content/uploads/Star-Wars-Logo-Art.jpg";

$files = array($image1, $image2);

$tmpFile = tempnam('/tmp', '');

$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($files as $file) {
    // download file
    $fileContent = file_get_contents($file);

    $zip->addFromString(basename($file), $fileContent);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=file.zip');
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);

unlink($tmpFile);

我写了个现成的,需要的同学可以直接拿来用。没有象楼上那样用zip直接在服务器压缩,楼的代码非常棒,但直接压缩我怕会比较受环境限制,可能不太适合作为一个通用的解决方案。
https://segmentfault.com/a/11...

推荐问题