php ZipArchive readfile 错误 没有这样的文件或目录 ziparchive


php ZipArchive readfile error No such file or directory ziparchive

我正在尝试使用 PHP/codeigniter 在 zip 存档中下载多个文件。此代码适用于我的本地主机,但在虚拟主机上,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);
    }

CodeIgniter 为此提供了一个 zip 库。它更容易处理,您可以将代码减少到最低限度。这也可能解决您的问题。祝你好运!