简体   繁体   English

在PHP中创建并下载zip

[英]Create and download zip in PHP

I have the following code: 我有以下代码:

    // start downloading
    require_once 'classes/class-FlxZipArchive.php';

    $filename = 'update_'.time();

    $path = 'updates'.DIRECTORY_SEPARATOR.$update;

    $zip = new FlxZipArchive;

    $zip->open($filename.'.zip',ZipArchive::CREATE);

    $zip->addDir($path,basename($path));

    $zip->close();

    if(file_exists($filename.'.zip')){

        header("Content-type: application/zip"); 

        header("Cache-Control: public");

        header("Content-Description: File Transfer");

        header("Content-Disposition: attachment; filename=".$filename.".zip");

        header("Content-length: ".filesize($filename.'.zip'));

        header("Pragma: no-cache"); 

        header("Expires: 0"); 

        readfile($filename.'.zip');

        unlink($filename.'.zip');

    };

    exit;

This works, but the problem is: not the zip is downloaded, but an unzipped version of the zip. 这可行,但是问题是:不是下载zip,而是下载了zip的未压缩版本。 The zip is created and is visible on the server, but it downloads an unzipped version of the zip. 该zip已创建并且在服务器上可见,但是会下载该zip的未压缩版本。

Let's say i have files.zip with the following content: 假设我有具有以下内容的files.zip:

- folder
    - images
        - image1.png
        - image2.png
    - docs
        - doc1.docx
        - doc2.docx

Then the zip is created but not downloaded. 然后创建了zip, 但未下载。 Instead, a normal map with name 'folder' is downloaded (unzipped). 而是下载(解压缩)名称为“ folder”的法线贴图。

Where is my mistake? 我的错误在哪里?

I use the following class: https://gist.github.com/panslaw/4327882 我使用以下类: https : //gist.github.com/panslaw/4327882

Edit: 编辑:

the code is working as expected, but I was using a mac with the settings to download and extract "save" files automatically. 该代码可以正常工作,但是我使用的Mac带有自动下载和提取“保存”文件的设置。 More info here: https://wiki.umbc.edu/pages/viewpage.action?pageId=31919091 此处提供更多信息: https : //wiki.umbc.edu/pages/viewpage.action?pageId=31919091

Please try that code : 请尝试该代码:

Html: HTML:

<div class='container'>
 <h1>Create and Download Zip file using PHP</h1>
 <form method='post' action=''>
  <input type='submit' name='create' value='Create Zip' />&nbsp;
  <input type='submit' name='download' value='Download' />
 </form>
</div>

Php p

I have created includes folder within the project where I stored some files and folders. 我在项目中创建了包含文件夹,在其中存储了一些文件和文件夹。

Create Zip 创建邮编

Create ZipArchive Class object for Zip file creation. 创建用于Zip文件创建的ZipArchive类对象。 Define a function createZip() to read files and directory from the specified directory path. 定义一个函数createZip()从指定目录路径读取文件和目录。

If file 如果文件

If the reading value is the file then add it to zip object using addFile() method. 如果读取值为文件,则使用addFile()方法将其添加到zip对象。

If directory 如果目录

If the value is directory then create an empty directory and call createZip() function where pass the directory path. 如果值为directory,则创建一个空目录并在传递目录路径的地方调用createZip()函数。

Download Zip 下载邮编

Check if zip file exists or not. 检查zip文件是否存在。 If it exists then download and remove it from the server. 如果存在,则从服务器下载并将其删除。

<?php 
// Create ZIP file
if(isset($_POST['create'])){
 $zip = new ZipArchive();
 $filename = "./myzipfile.zip";

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

 $dir = 'includes/';

 // Create zip
 createZip($zip,$dir);

 $zip->close();
}

// Create zip
function createZip($zip,$dir){
 if (is_dir($dir)){

  if ($dh = opendir($dir)){
   while (($file = readdir($dh)) !== false){

    // If file
    if (is_file($dir.$file)) {
     if($file != '' && $file != '.' && $file != '..'){

      $zip->addFile($dir.$file);
     }
    }else{
     // If directory
     if(is_dir($dir.$file) ){

      if($file != '' && $file != '.' && $file != '..'){

       // Add empty directory
       $zip->addEmptyDir($dir.$file);

       $folder = $dir.$file.'/';

       // Read data of the folder
       createZip($zip,$folder);
      }
     }

    }

   }
   closedir($dh);
  }
 }
}

// Download Created Zip file
if(isset($_POST['download'])){

 $filename = "myzipfile.zip";

 if (file_exists($filename)) {
  header('Content-Type: application/zip');
  header('Content-Disposition: attachment; filename="'.basename($filename).'"');
  header('Content-Length: ' . filesize($filename));

  flush();
  readfile($filename);
  // delete file
  unlink($filename);

 }
}
?>

CSS 的CSS

.container{
 margin: 0 auto;
 width: 50%;
 text-align: center;
}

input[type=submit]{
 border: none;
 padding: 7px 15px;
 font-size: 16px;
 background-color: #00a1a1;
 color: white;
 font-weight: bold;
}

Check that link 检查该链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM