使用 php 创建和下载 zip 文件 [英] create and download zip file using php

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

问题描述

我正在尝试为此创建一个 zip 文件(使用 php),我编写了以下代码:

i am trying to create a zip file(using php) for this i have written the following code:

$fileName = "1.docx,2.docx";
$fileNames = explode(',', $fileName);
$zipName = 'download_resume.zip';
$resumePath = asset_url() . "uploads/resume/";
//http://localhost/mywebsite/public/uploads/resume/

$zip = new ZipArchive();
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
    echo json_encode("Cannot Open");
}

foreach ($fileNames as $files) {
    $zip->addFile($resumePath . $files, $files);
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zipName."");
header("Content-length: " . filesize($zipName));
header("Pragma: no-cache"); 
header("Expires: 0");
readfile($zipName);
exit;

但是在单击按钮时,我没有收到任何信息..甚至没有任何错误或消息..

however on a button click i am not getting anything..not even any error or message..

任何帮助或建议对我来说都是一个很大的帮助..提前致谢

any help or suggestion would be a great help for me.. thanks in advance

推荐答案

为什么不使用 Codeigniter 中的 Zip Encoding Class - 它会为您做到这一点

Why not use the Zip Encoding Class in Codeigniter - it will do this for you

$name = 'mydata1.txt';
$data = 'A Data String!';

$this->zip->add_data($name, $data);

// Write the zip file to a folder on your server. Name it "my_backup.zip"
$this->zip->archive('/path/to/directory/my_backup.zip'); 

// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

https://www.codeigniter.com/user_guide/libraries/zip.html

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

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