0

I have an action in my cakephp controller with zip function (from How to [recursively] Zip a directory in PHP?) :

    public function documents() {

        // PAGE VIERGE
        $this->layout = null;
        $this->autoRender = false;

        // CALL TEST If data is sended
        if (!empty($this->request->data) && ($this->request->data['call'] == 'yes')) {

            $zip = new ZipArchive();

            // callOrigin separate way
            if ($this->request->data['callOrigin'] == 'dmc') {

                $filename = $_SERVER['DOCUMENT_ROOT'] . '/resources/data/documents/documentMC.zip';
                if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
                    exit ('Impossible d\'ouvrir le fichier '.$filename.'');
                }
                // SEE http://php.net//manual/fr/ziparchive.addfile.php
                $zip->addFile($_SERVER['DOCUMENT_ROOT'] . '/resources/data/documents/documentMC.html', "document/megacolon.html");

            } elseif ($this->request->data['callOrigin'] == 'dsc') {

                $filename = $_SERVER['DOCUMENT_ROOT'] . '/resources/data/documents/documentSC.zip';
                if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
                    exit ('Impossible d\'ouvrir le fichier '.$filename.'');
                }
                // SEE http://php.net//manual/fr/ziparchive.addfile.php
                $zip->addFile($_SERVER['DOCUMENT_ROOT'] . '/resources/data/documents/documentSC.html', "document/suicide.html");

            } else {
                return false;
            }





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

            // Ignore "." and ".." folders
            if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
                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();
}


            // zip libs
            Zip($_SERVER['DOCUMENT_ROOT'] . '/resources/libs/font-awesome/', $filename);
            Zip($_SERVER['DOCUMENT_ROOT'] . '/resources/libs/jquery/', $filename);
            Zip($_SERVER['DOCUMENT_ROOT'] . '/resources/libs/bootstrap/', $filename);

            // zip individuals
            $zip->addFile($_SERVER['DOCUMENT_ROOT'] . '/resources/data/documents/README.md', "README.md");
            $zip->addFile($_SERVER['DOCUMENT_ROOT'] . '/resources/css/DOCS/baseDocs.css', "resources/css/DOCS/baseDocs.css");

            // close zip
            $zip->close();

        }
}

With no errors msg in my logs, only the files .html, README and css are in the zip, zip function seems to be not performed... what is my mistake ?

Community
  • 1
  • 1
zelocalhost
  • 1,175
  • 3
  • 20
  • 39
  • Well, can you give us more detail? Maybe a link to it? Or a screenshot of the files? I personally think the problem may be you're using .html files. Servers aren't typically configured to process .html files with PHP. – Mikel Bitson Dec 03 '14 at 01:53
  • There is no .html files to zip in "zip libs" section (jquery, bootstrap, font-awesome) with Zip function, the .html file documentXX is ziped correctly outside this function. – zelocalhost Dec 03 '14 at 14:41

0 Answers0