使用php创建和下载zip文件

Moh*_*ian 3 php zip codeigniter download

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

$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;
Run Code Online (Sandbox Code Playgroud)

但是在按钮点击我没有得到任何东西..甚至没有任何错误或消息..

任何帮助或建议对我都有很大的帮助..在此先感谢

Zab*_*abs 6

为什么不在Codeigniter中使用Zip编码类 - 它会为您执行此操作

$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');
Run Code Online (Sandbox Code Playgroud)

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