用PHP创建和下载zip

时间:2018-01-04 12:35:59

标签: php zip

我有以下代码:

    // 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;

这样可行,但问题是:没有下载zip,而是拉链的解压缩版本。 zip已创建并在服务器上可见,但它会下载解压缩的zip版本。

假设我有files.zip,内容如下:

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

然后创建了zip,但已下载。而是下载(解压缩)名为“文件夹”的普通地图。

我的错误在哪里?

我使用以下课程:https://gist.github.com/panslaw/4327882

修改

代码按预期工作,但我使用带有设置的mac来自动下载和提取“保存”文件。更多信息:https://wiki.umbc.edu/pages/viewpage.action?pageId=31919091

1 个答案:

答案 0 :(得分:0)

请尝试该代码:

<强> 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>

<强>腓

我在项目中创建了包含文件夹,其中存储了一些文件和文件夹。

创建Zip

为Zip文件创建创建ZipArchive Class对象。定义一个函数createZip()以从指定的目录路径中读取文件和目录。

如果文件

如果读取值是文件,则使用addFile()方法将其添加到zip对象。

如果目录

如果值是directory,则创建一个空目录并调用createZip()函数,其中传递目录路径。

下载Zip

检查zip文件是否存在。如果存在,则下载并从服务器中删除它。

<?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

.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;
}

检查link

相关问题