使用PHP创建zip,从url添加文件 [英] Create zip with PHP adding files from url

查看:109
本文介绍了使用PHP创建zip,从url添加文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下是否可以做,希望有人可以帮助我。

I was wondering if the following is possible to do and with hope someone could potentially help me.

我想创建一个下载zip功能,但是单独点击下载,然后按钮从我的外部域中获取图像,然后将其捆绑成一个zip,然后下载它们。

I would like to create a 'download zip' feature but when the individual clicks to download then the button fetches images from my external domain and then bundles them into a zip and then downloads it for them.

我已经检查了如何做这个和我找不到任何好的方式来抓取图像,并强迫他们进入zip下载。

I have checked on how to do this and I can't find any good ways of grabbing the images and forcing them into a zip to download.

我希望有人可以协助

推荐答案

# define file array
$files = array(
    'http://google.com/images/logo.png',
    'http://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Wikipedia-logo-en-big.png/220px-Wikipedia-logo-en-big.png',
);

# create new zip object
$zip = new ZipArchive();

# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);

# loop through each file
foreach ($files as $file) {
    # download file
    $download_file = file_get_contents($file);

    #add it to the zip
    $zip->addFromString(basename($file), $download_file);
}

# close zip
$zip->close();

# send the file to the browser as a download
header('Content-disposition: attachment; filename="my file.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);

注意:此解决方案假定您有 allow_url_fopen 启用。否则请使用cURL下载文件。

Note: This solution assumes you have allow_url_fopen enabled. Otherwise look into using cURL to download the file.

这篇关于使用PHP创建zip,从url添加文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆