Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php代码压缩并自动下载一个文件。在Windows中工作,但在Ubuntu中不工作_Php_Apache_Download_Ubuntu 12.04_Ziparchive - Fatal编程技术网

php代码压缩并自动下载一个文件。在Windows中工作,但在Ubuntu中不工作

php代码压缩并自动下载一个文件。在Windows中工作,但在Ubuntu中不工作,php,apache,download,ubuntu-12.04,ziparchive,Php,Apache,Download,Ubuntu 12.04,Ziparchive,我有一个关于在列表中显示根目录的所有文件的项目,用户可以浏览文件目录,按名称搜索文件,使用downloadAll按钮,他可以下载当前列表中的所有文件。要使downloadAll按钮正常工作,我使用downloadAll.php代码,通过获取当前目录,可以创建一个zip文件并自动下载。它在windows中工作正常。我试图在linux上运行相同的项目,但在这种情况下,它会下载一个空的压缩文件。此外,当我尝试打开它时,会显示以下消息: Archive: /tmp/archived_name.zip

我有一个关于在列表中显示根目录的所有文件的项目,用户可以浏览文件目录,按名称搜索文件,使用downloadAll按钮,他可以下载当前列表中的所有文件。要使downloadAll按钮正常工作,我使用downloadAll.php代码,通过获取当前目录,可以创建一个zip文件并自动下载。它在windows中工作正常。我试图在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.
下载zip.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");
?>


您可能在其他地方发生了错误。尝试将
$download\u file
设置为false,并将
错误报告(E\u ALL)
设置为查看是否存在任何错误。如果这不起作用,请尝试查看web服务器错误日志。对于apache,它们默认为
/var/log/apache2/error.log

“空zip文件”对您的意义可能与我不同。该文件是否实际为空(以零字节为单位)?或者你的zip程序就是看不到文件?我假设zip文件中可能抛出了一些php错误。我建议在文本编辑器中打开zip文件并查找php错误。我用txt编辑器打开了该文件,但该文件为空。因此,您可能在其他地方发生了错误。尝试将
$download\u file
设置为false,并将
错误报告(E\u ALL)
设置为查看是否存在任何错误。如果这不起作用,请尝试查看web服务器错误日志。对于apache,默认值为
/var/log/apache2/access.log
或类似值。首先感谢您抽出时间。我不知道如何设置error_reporting(E_ALL),但在/var/log/apache2/access.log中,当我试图在出现的第一个屏幕(根目录)中使用download ALL时出现的最后一行是192.168.1.5---[29/Sep/2014:20:32:51+0300]“GET/my_project/downloadZip.php?currentdirectory=HTTP/1.1“200 435”“Mozilla/5.0”(X11;Ubuntu;Linux i686;rv:32.0)Gecko/20100101 Firefox/32.0“问题实际上在于我的项目目录权限。感谢您抽出时间:/