0

I'm trying to download multiple files in a zip archive using PHP/codeigniter.This code works on my localhost but on the webhost the zip starts download but the files are not added to the zip file. This is the error I got:

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

This is my code:

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);
    }
4

1 に答える 1

1

CodeIgniter provides a zip library for this purpose. It's a lot easier to handle and you can reduce your code to a minimum. This might also solve your problem. Good luck!

于 2012-06-13T17:28:57.077 に答える