dov6891 2014-09-29 17:11
浏览 33
已采纳

php代码压缩和自动下载文件。 适用于Windows,但不适用于Ubuntu

I've got a project about showing all files of root directory in a list and the user can browse through files-directories, search a file by name,with the button downloadAll he can download all files at the current list.To make the downloadAll button work I use downloadAll.php code which by getting the current directory can create a zip file and auto download it. It works fine in windows. I'm trying to run the same project at linux but in that case it downloads an empty zipped file. Also when I try to open it this message appears:

Archive:  /tmp/archived_name.zip
[/tmp/archived_name.zip]
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
zipinfo:  cannot find zipfile directory in one of /tmp/archived_name.zip or
          /tmp/archived_name.zip.zip, and cannot find /tmp/archived_name.zip.ZIP, period.

downloadZip.php:

<?php
if ((isset($_GET['currentdirectory'])) && ($_GET['currentdirectory']!="")){
    $the_folder = $_GET['currentdirectory']; 
}
else {
    $the_folder = "/var/www/my_project";
}

    $the_folder1 = $the_folder;

$zip_file_name = 'archived_name.zip';

$download_file= true;

class FlxZipArchive extends ZipArchive {

    public function addDir($location, $name) {
        $this->addEmptyDir(str_replace("./","",$name));

        $this->addDirDo($location, str_replace("./","",$name));
    } 

    private function addDirDo($location, $name) {
        $name .= '/';
        $location .= '/';

        $dir = opendir ($location);
        while ($file = readdir($dir))
        {
            if ($file == '.' || $file == '..') continue;

            $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';

                $this->$do($location . $file, str_replace("./","",$name) . $file);
        }
    } 
}

$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE) 
{
    $za->addDir($the_folder1, basename($the_folder1));
    $za->close();
}
else  { echo 'Could not create a zip archive';}

if ($download_file)
{
    ob_get_clean();
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($zip_file_name));
    readfile($zip_file_name);
}
unlink("archived_name.zip");
?>
  • 写回答

1条回答 默认 最新

  • doubu8643 2014-09-29 21:55
    关注

    You likely have an error that is happening elsewhere. Try setting $download_file to false and set error_reporting(E_ALL) to see if there are any errors. If that doesn't work, try viewing the web server error logs. For apache they default to /var/log/apache2/error.log.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改