简体   繁体   中英

Codeigniter Zip directory and download

I am having a very strange problem when zipping a directory and try to download using codeigniter.

Here is the code

    $this->load->library('zip'); //Loading the zip library
    $directory = $_SESSION['directory-download-path']; //Getting the directory from session
    $name = basename($directory); //get the name of the folder
    $name = str_replace(" ", "_", $name).".zip"; //create the zip name
    unset($_SESSION['directory-download-path']); //removing it from session
    $this->zip->read_dir($directory); //read the directory
    $this->zip->download($name); //download the zip

It is very simple. The problem occurs with the download. I get the zip file but when i extract it i get a file with .zip.cpgz and continues to extract similar files. Thus i think it is corrupted. Can you please help me why is this occurring. I have permissions and everything on the directory because i am doing other operations.

EDIT:

I found after some more research to add the second parameter as follows:

$this->zip->read_dir($directory, false); //read the directory

But still not working. Another solution was to add

ob_end_clean(); 

just before the line: $this->zip->download($name); //download the zip $this->zip->download($name); //download the zip

but still no success!

Since I had no response i made a function outside of codeigniter that zips files and folder recursively which is below:

public function Zip($source, $destination)
{
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

    $zip = new ZipArchive();
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
        return false;
    }

    $source = str_replace('\\', '/', realpath($source));

    if (is_dir($source) === true)
    {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

        foreach ($files as $file)
        {
            $file = str_replace('\\', '/', $file);

            if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..', "Thumbs.db")) )
                continue;

            $file = realpath($file);

            if (is_dir($file) === true)
            {
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
            }
            else if (is_file($file) === true)
            {
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            }
        }
    }
    else if (is_file($source) === true)
    {
        $zip->addFromString(basename($source), file_get_contents($source));
    }

    return $zip->close();
}

After this in the codeigniter controller i made this:

$this->projects->Zip($source, $destination);
   header("Content-type: application/zip");
   header("Content-Disposition: attachment; filename=$name");
   header("Content-length: " . filesize($destination));
   header("Pragma: no-cache");
   header("Expires: 0");
   readfile($destination);
   unlink($destination);

Of course the $destination should be to a temp folder where you have permissions 777 or equivalent so that when the file is sent to the client it will be deleted.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM