[英]download zip file php
因此,我一直试图从WAMP服务器创建并下载.zip文件,以在正在使用的私人开发服务器上使用它,并且在尝试下载已创建的.zip文件时似乎遇到了一些主要问题。
该代码似乎按预期工作。 它浏览一个包含我要下载的文件的文件夹,并将它们添加到要由我的zipper类使用的数组中(底部附近的函数调用)。
当我运行此代码时,将在服务器上创建.zip文件。 我可以看到它在那里,打开它,看到文件已添加到.zip中。 同样,我可以将压缩文件夹从本地wamp服务器上拖下,然后从我的客户端计算机中提取出来。 问题是当我尝试下载压缩文件时。 它似乎正在下载文件,但是当我打开它时,出现一个Windows框,显示“ Windows无法打开文件夹,压缩(压缩)文件夹/ path&name /无效”,但在服务器端工作正常。
我也尝试过使用这种方案来下载和查看单个文件(非压缩),并且它们总是以随机文本的形式出现,这使我相信这与将文件从服务器下载到客户端计算机有关。
同样,我尝试在实时服务器上运行此代码。 尝试查看时,会发生同样的事情,除了(在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
}
}
}
}
任何帮助,将不胜感激。
<?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
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.