简体   繁体   中英

download zip file php

So I have been attempting to create and Download a .zip file from my WAMP server to be used on a private development server I am working on, and seem to have hit some major issues when trying to download the .zip file I have created.

The code appears to work as intended. It looks through a folder which contains the files I want downloaded, and adds them to an array to be used by my zipper class (function calls near the bottom).

When I run this code the .zip file is created on the server; I can see that it is there, open it, see that the files were added to the .zip. As well i can drag the zipped folder off of my local wamp server and extract it from my client machine fine. The problem is when I try and download the compressed file. It appears to download the file, but when I go to open it a Windows box appears saying "Windows cannot open the folder, The Compressed (zipped) folder /path&name/ is invalid", yet it works fine on the server end.

I have attempted using this scheme to download and view single files as well (Non zipped) and they always come out as random text, which leads me to believe that it has something to do with serving the download from the server to the client machine.

As well I have attempted to run this code on my live server. Same thing happens, when trying to view, except that (in chrome) a message saying "/filename/ is not commonly downloaded and may be dangerous".

I have checked various posting on stackoverflow for a solution and non seem to help.

<?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");
?>

Below is the "zipper.php" file that I used.

<?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
        }
    }
}   
}

Any help would be appreciated.

<?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
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM