我正在尝试使用PHP / codeigniter在zip存档中下载多个文件。此代码适用于我的localhost但在webhost上zip开始下载,但文件未添加到zip文件中。这是我得到的错误:
A PHP Error was encountered
Severity: Warning
Message: readfile(Album1.zip): failed to open stream: No such file or directory
Filename: controllers/download.php
Line Number: 54
这是我的代码:
function _zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
if(file_exists($archive_file_name)){
if ($zip->open($archive_file_name, ZIPARCHIVE::OVERWRITE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
}else
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
//echo $file_path.$files."<br>";
}
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
function album($albumid){
$this->load->model("albums_model");
$this->load->model("songs_model");
$aid = $albumid;
$albuminfo = $this->albums_model->getAlbum($aid);
$songs = $this->songs_model->getSongNamesAvailableDownload($aid);
$file_path=$_SERVER['DOCUMENT_ROOT']. '/demo/albums/'.$albumid.'/';
$archive_file_name = $albuminfo->album_name;
$file_names = $songs;
$this->_zipFilesAndDownload($file_names,$archive_file_name,$file_path);
}