所以我一直在試圖從我的WAMP服務器上創建和下載一個.zip文件,以便在我正在使用的私人開發服務器上使用,並且似乎在嘗試下載.zip文件我已經創建。下載zip文件php
該代碼似乎按預期工作。它通過一個包含我想下載的文件的文件夾來查看,並將它們添加到我的拉鍊類(靠近底部的函數調用)使用的數組中。
當我運行此代碼時,服務器上創建.zip文件;我可以看到它在那裏,打開它,看到這些文件被添加到.zip中。以及我可以將壓縮文件夾從我的本地wamp服務器上拖出來,並從我的客戶機上提取它。 問題是當我嘗試下載壓縮文件時。它似乎下載文件,但當我打開它時出現一個Windows框,說「Windows無法打開文件夾,壓縮(壓縮)文件夾/路徑&名稱/無效」,但它在服務器端正常工作。
我一直在使用這個方案來下載和查看單個文件,以及(非壓縮),他們總是站出來爲隨機文本嘗試,這使我相信,它是與從服務器服務下載到客戶機。
以及我試圖在我的活服務器上運行此代碼。同樣的事情發生時,試圖查看,除了(在鉻)一條消息說「/文件名/不常被下載,可能是危險的」。
我已經檢查了各種發佈在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
}
}
}
}
任何幫助,將不勝感激。
這真的很奇怪,我評論了你在你做過的標題,甚至還沒有工作。你是否也可以在一個Wamp/Lamp服務器上運行你的?我想知道如果它的錯誤與我的PHP配置。 –
我在wamp上測試過,我確實把Content-Disposition放回到一行中。我第一次嘗試下載與php文件同名的附件 – bumperbox
我已經下載了「download.php」文件。對我來說,它確實下載了「myFile.zip」它只是因爲任何原因而無法打開。留下您留下的標題仍然以一個無法打開的zip文件結尾。奇怪的問題,但你做了一個名爲「文件」的文件夾,並用幾個不同的文件填充它? –