如何在CakePhp中创建Zip文件并下载? [英] How to create Zip file and download in CakePhp?

查看:81
本文介绍了如何在CakePhp中创建Zip文件并下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CakePhp 1.3创建zip文件。文件描述存储在数据库中。在我的控制器中,我使用以下逻辑。

I'm trying to create zip files using CakePhp 1.3. The file descriptions are stored in database. In my controller I'm using the following logic.

   // Fetching File description form database.     
   $this->view = 'Media';
   $user_id=$this->Session->read('userData.User.id');
   $condition = "Document.isdeleted = '0' AND Document.project_id='".  $project_id."'";
   $projectDocumentList = $this->Document->find("all",array('conditions'=>$condition,'fields'=>array('Document.id','Document.document_name','Document.path'),'order' => array('Document.id ASC')));
   $this->set('projectDocumentList',$projectDocumentList);
   if(!empty($projectDocumentList)){
   $fileNames = "";
   $fileNamesArr = array();
    foreach($projectDocumentList as $projectDocument){     
     $fileNamesArr[ ]= $projectDocument['Document']['document_name'];
    }
   }

   // Making zip
   $archive_file_name='myFile.zip';
   $file_path= "my_file_path";// Here I'm using my server basepath

   $zip = new ZipArchive();   
   if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
    exit("cannot open <$archive_file_name>\n");
   }

   foreach($fileNamesArr as $files)
   {
   $zip->addFile($file_path.$files,$files);
   //echo $file_path.$files."<br>"; // This can show the file in browser
   }

   $zip->close();
   header("Content-type: application/zip");
   header("Content-Disposition: attachment; filename=$archive_file_name");
   header("Pragma: no-cache");
   header("Expires: 0");
   readfile("$archive_file_name");
   exit;

现在将zip文件下载为myFile.zip,但是当我打开该文件时,它会引发错误归档文件格式未知或已损坏。

Now the zip file download as myFile.zip, but when I'm going to open that file, it throws an error "The archive is either in unknown format or damaged".

推荐答案

创建整个文件夹的zip文件



Create Zip File of whole folder

// Get real path for our folder
$rootPath = realpath('folder-to-zip');

// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}

// Zip archive will be created only after closing object
$zip->close();

然后将zip文件的路径传递给视图。

And than pass the path of the zip file to the view.

$this->set('Zip_path'.zip_file_name);

这篇关于如何在CakePhp中创建Zip文件并下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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