PHP代码压缩和自动下载文件。作品在Windows,但没有在Ubuntu

问题描述 投票:0回答:1

我有一个项目大约显示列表根目录下的所有文件和用户可以通过文件,目录浏览,按名称搜索文件,与按钮downloadAll他可以下载所有的文件在当前list.To使downloadAll按钮的工作我用downloadAll.php代码,通过获取的当前目​​录可以创建一个zip文件,并自动下载。它在窗口工作正常。我试图运行在Linux上的同一个项目,但是在这种情况下,它下载一个空的压缩文件。此外,当我尝试打开它出现此消息:

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");
?>
php apache download ubuntu-12.04 ziparchive
1个回答
1
投票

你可能有一个在其他地方发生的错误。尝试$download_file设置为false,并设置error_reporting(E_ALL),看看是否有任何错误。如果还是不行,请尝试查看Web服务器错误日志。对于Apache它们默认为/var/log/apache2/error.log

© www.soinside.com 2019 - 2024. All rights reserved.