【问题标题】:Unable to view files inside Zip (But has Memory) created using ZipArchive function of php无法查看使用 php 的 ZipArchive 函数创建的 Zip 内的文件(但有内存)
【发布时间】:2012-12-11 01:41:37
【问题描述】:
<?php
       $error = "";
       $file_folder = "temp/";
        // folder to load files
 if (extension_loaded('zip')) {
    // Checking ZIP extension is available
    $zip = new ZipArchive();
    // Load zip library
    $zip_name = "images_".date("d-m-Y") . ".zip";
    // Zip name
    if ($zip -> open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) {
        // Opening zip file to load files
        $error .= "* Sorry ZIP creation failed at this time";
    }
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../uploads/"));
    $files = iterator_to_array($iterator, true);

    foreach ($iterator as $file) {
        $zip -> addFile($file_folder . $file);
        // Adding files into zip
    }
    print_r($zip);
    //echo $zip_name;
    if (file_exists($file_folder.$zip_name)) {
        // push to download the zip
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="' . $zip_name . '"');
        readfile($zip_name);
        // remove zip file is exists in temp path
        unlink($zip_name);
    } else {
        echo "error";
    }

    $zip -> close();
//} else
//  $error .= "* Please select file to zip ";
 } else
$error .= "* You dont have ZIP extension";

?>

我尝试输出 $zip 对象并得到以下响应: ZipArchive 对象([status] => 0 [statusSys] => 0 [numFiles] => 12 [filename] => /var/www/bigb/ajax/images_24-12-2012.zip [comment] =>)

状态为零说明创建 zip 时没有错误(参考:http://www.php.net/manual/en/zip.constants.php)

我参考了以下帖子: 1)Download all images from a single directory of a website 2)How to create a zip file using php and delete it after user downloads it? 3)http://www.9lessons.info/2012/06/creating-zip-file-with-php.html

第三个对我帮助最大,另外两个我的状态是 23。

问题:我可以创建一个 zip 并且下载它没有问题,但是当我打开 zip 时,我看不到里面的任何文件,但 zip 有 mbs 中的内存。

请帮帮我...

更新:由于 print_r / echo 和/或试图覆盖同一文件而发生错误 23。

Update2:问题已解决。这是由于 RecursiveDirectoryIterator 中的路径(即 ../uploads/),一旦我将代码移动到主文件夹并将路径更改为(uploads/),一切都开始正常工作。赞!!!

【问题讨论】:

    标签: php zip


    【解决方案1】:

    这是由于 RecursiveDirectoryIterator 中的路径(即../uploads/),一旦我将代码移动到主文件夹并将路径更改为(uploads/),一切都开始正常工作。赞!!!

    【讨论】:

      【解决方案2】:

      我认为您提供给$zip-&gt;addFile() 的文件路径不正确。您无需将任何内容附加到 $file。以下将起作用:

      foreach ($iterator as $file) {
          $zip -> addFile($file);
      }
      

      在您的情况下,$file 将包含文件的确切相对路径,例如 ../uploads/file.ext。

      【讨论】:

      • 感谢您的回复。我都试过了。我的服务器没有写权限,所以我添加了一个可以写的文件夹。但是在打开 zip 时,我没有给出正确的文件夹(这可能是一个问题),但它不能正常工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多