We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40385
    • 75 Posts
    Hello,

    I am using the following code to create a zip file of the template folder (assets/template), download it and then delete the zip file. When I store this code in a file called export.php and put it into the root folder of my MODx installation everything works as expected. I am not a big fan of external scripts while using MODx, so I stored this code in a snippet and called this snippet in a resource named export. So everytime the user opens http://mysite.com/export.html the zip file should be created....but it is not working. Instead I receive a

    Failed to load resource: net::ERR_INVALID_RESPONSE

    Any idea why this is working outside of MODx but not inside with a snippet?

    <?php
    
    $source = 'assets/template';
    $destination = 'files.zip';
    
    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));
    }
    
    $zip->close();
    
    if(file_exists($destination)){
            // force to download the zip
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false);
            header('Content-type: application/zip');
            header('Content-Disposition: attachment; filename="'.$destination.'"');
            readfile($destination);
            // remove zip file from temp path
            unlink($destination);
        } else {
            echo "No valid files to zip";
            exit;
    }

    This question has been answered by paul_kemp. See the first response.

    [ed. note: paul_kemp last edited this post 9 years, 1 month ago.]
    • discuss.answer
      • 40385
      • 75 Posts
      The last post in here fixed my problem:

      http://forums.modx.com/index.php/topic,38940.msg235731.html