下载zip文件php

时间:2017-04-05 00:24:17

标签: php apache download zip

所以我一直在尝试从我的WAMP服务器创建和下载.zip文件,以便在我正在使用的私有开发服务器上使用,并且在尝试下载.zip文件时似乎遇到了一些重大问题我已经创造出来了。

代码似乎按预期工作。它查看包含我想要下载的文件的文件夹,并将它们添加到我的zipper类(靠近底部的函数调用)使用的数组中。

当我运行此代码时,会在服务器上创建.zip文件;我可以看到它在那里,打开它,看到文件被添加到.zip。我也可以将压缩文件夹拖离我的本地wamp服务器并从我的客户端机器中提取出来。 问题是当我尝试下载压缩文件时。它似乎下载了该文件,但是当我打开它时,会出现一个Windows框,说“Windows无法打开文件夹,压缩(压缩)文件夹/路径&名称/无效”,但它在服务器端正常工作。

我曾尝试使用此方案下载和查看单个文件(非压缩),它们总是以随机文本形式出现,这让我相信它与从服务器提供服务到下载有关客户机器。

我也尝试在我的实时服务器上运行此代码。在尝试查看时会发生同样的事情,除了(在chrome中)一条消息“/ filename /通常不会被下载并且可能是危险的”。

我已经检查了stackoverflow上的各种帖子以获得解决方案,而且似乎没有帮助。

<?php

require_once 'zipper.php';
$dir_path = "files/";

$arrFiles = array(); //Array Of files to be compressed into zip

if(is_dir($dir_path)){
    $files = scandir($dir_path); //create and array that stores all dir files.directories including '.' and '..'
    foreach($files as $newFile){ //Loop through the files
        if($newFile != '.' && $newFile != '..'){ //If the current found is NOT . or ..
            echo "$newFile<br>";
            $newFile = $dir_path . $newFile; 
            $arrFiles[] = $newFile; //add to the array of files to compress/zip
        }
    }
}

//print_r($dir_path."name.zip");

$zipper = new Zipper; //create the zipper object (contains functions for preparing the .ZIP)
$zipper->add($arrFiles); //add files to Zipper ZIP array

$filename = "myFile.zip";
$filepath = $dir_path.$filename; //Path to the new .zip with the ZIP name included in the path
echo $filepath;
echo "<br>";

$zipper->store($filepath); //create the ZIP & store for download

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($filepath).";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filepath));
    readfile("$filepath");
?>

下面是我使用的“zipper.php”文件。

<?php
class Zipper{
private $_files = array(), $_zip;

public function __construct(){
    $this->_zip = new ZipArchive;
}

public function add($input){
    if(is_array($input)){
        $this->_files = array_merge($this->_files, $input);
    }else{
        $this->_files[] = $input;
    }
}

public function store($location = null){ //used for storing the files added from the directory
    if(count($this->_files) && $location){
        foreach($this->_files as $index => $file){
            if(!file_exists($file)){
                unset($this->_files[$index]);
            }
            elseif(preg_match('/.zip$/', $file)){ //if the file was already compressed once, remove it from the array of files to zip
                unset($this->_files[$index]);
            }
        }
        print_r($this->_files);
        if($this->_zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){ //create the .ZIP; if it exists overwrite, else create it

            foreach($this->_files as $file){
                $this->_zip->addFile($file, $file); //for each file in our array add it to the zip
            }

            $this->_zip->close(); //close the zip once completed
        }
    }
}   
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

<?php

$dir_path = "files/";

$arrFiles = array(); //Array Of files to be compressed into zip

if (is_dir($dir_path)) {
    $files = scandir($dir_path); //create and array that stores all dir files.directories including '.' and '..'
    foreach ($files as $newFile) { //Loop through the files
        if ($newFile != '.'&&$newFile != '..') { //If the current found is NOT . or ..
//            echo "$newFile<br>";
            $newFile = $dir_path . $newFile;
            $arrFiles[] = $newFile; //add to the array of files to compress/zip
        }
    }
}

//print_r($dir_path."name.zip");

$zipper = new Zipper; //create the zipper object (contains functions for preparing the .ZIP)
$zipper->add($arrFiles); //add files to Zipper ZIP array

$filename = "myFile.zip";
$filepath = $dir_path . $filename; //Path to the new .zip with the ZIP name included in the path
//echo $filepath;
//echo "<br>";

$zipper->store($filepath); //create the ZIP & store for download

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($filepath) . ";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filepath));
readfile("$filepath");

class Zipper
{
    private $_files = array(), $_zip;

    public function __construct()
    {
        $this->_zip = new ZipArchive;
    }

    public function add($input)
    {
        if (is_array($input)) {
            $this->_files = array_merge($this->_files, $input);
        } else {
            $this->_files[] = $input;
        }
    }

    public function store($location = null)
    { //used for storing the files added from the directory
        if (count($this->_files)&&$location) {
            foreach ($this->_files as $index => $file) {
                if (!file_exists($file)) {
                    unset($this->_files[$index]);
                } elseif (preg_match('/.zip$/', $file)) { //if the file was already compressed once, remove it from the array of files to zip
                    unset($this->_files[$index]);
                }
            }
//            print_r($this->_files);
            if ($this->_zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)) { //create the .ZIP; if it exists overwrite, else create it

                foreach ($this->_files as $file) {
                    $this->_zip->addFile($file, $file); //for each file in our array add it to the zip
                }

                $this->_zip->close(); //close the zip once completed
            }
        }
    }
}