0
我想下载zip文件中的多个文件使用PHP/codeigniter.This代码工作在我的本地主机,但在网络主机的zip开始下载,但文件没有添加到zip文件。这是我得到的错误:php ZipArchive读取文件错误没有这样的文件或目录ziparchive
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);
}