src/sb/Files/ForceDownload.php
public static function fileToZip($path)
    {
        if (is_file($path) || is_dir($path)) {
            $zip = new \ZipArchive;
            $zip_file = ROOT . '/private/cache/zip/' . md5(microtime(true));
            if (!is_dir(dirname($zip_file))) {
                mkdir(dirname($zip_file), 0775, true);
            }

            if ($zip->open($zip_file, \ZipArchive::CREATE) === true) {
                if (is_dir($path)) {
                    $iterator = new \DirectoryIterator($path);

                    foreach ($iterator as $file) {
                        if ($file->isFile()) {
                            $bn = $file->getBasename();
                            $zip->addFile($file->getPath() . '/' . $bn, $bn);
                        }
                    }
                } else {
                    $zip->addFile($path, basename($path));
                }

                if ($zip->close()) {
                    self::send($zip_file, str_replace("/", "_", basename($path)) . '.zip');
                    unlink($zip_file);
                }
            } else {
                throw(new \Exception('failed to create zip file'));
            }
        } else {
            throw(new \Exception('No data found!'));
        }
    }
public static function fileToZip($path)
    {
        if (is_file($path) || is_dir($path)) {
            $zip = new \ZipArchive;
            $zip_file = ROOT . '/private/cache/zip/' . md5(microtime(true));
            if (!is_dir(dirname($zip_file))) {
                mkdir(dirname($zip_file), 0775, true);
            }

            if ($zip->open($zip_file, \ZipArchive::CREATE) === true) {
                if (is_dir($path)) {
                    $iterator = new \DirectoryIterator($path);

                    foreach ($iterator as $file) {
                        if ($file->isFile()) {
                            $bn = $file->getBasename();
                            $zip->addFile($file->getPath() . '/' . $bn, $bn);
                        }
                    }
                } else {
                    $zip->addFile($path, basename($path));
                }

                if ($zip->close()) {
                    self::send($zip_file, str_replace("/", "_", basename($path)) . '.zip');
                    unlink($zip_file);
                }
            } else {
                throw(new \Exception('failed to create zip file'));
            }
        } else {
            throw(new \Exception('No data found!'));
        }
    }
public static function fileToZip ( $ path ) { if ( is_file ( $ path ) || is_dir ( $ path ) ) { $ zip = new \ ZipArchive ; $ zip_file = ROOT . '/private/cache/zip/' . md5 ( microtime ( true ) ) ; if ( ! is_dir ( dirname ( $ zip_file ) ) ) { mkdir ( dirname ( $ zip_file ) , 0775 , true ) ; } if ( $ zip -> open ( $ zip_file , \ ZipArchive :: CREATE ) === true ) { if ( is_dir ( $ path ) ) { $ iterator = new \ DirectoryIterator ( $ path ) ; foreach ( $ iterator as $ file ) { if ( $ file -> isFile ( ) ) { $ bn = $ file -> getBasename ( ) ; $ zip -> addFile ( $ file -> getPath ( ) . '/' . $ bn , $ bn ) ; } } } else { $ zip -> addFile ( $ path , basename ( $ path ) ) ; } if ( $ zip -> close ( ) ) { self :: send ( $ zip_file , str_replace ( "/" , "_" , basename ( $ path ) ) . '.zip' ) ; unlink ( $ zip_file ) ; } } else { throw ( new \ Exception ( 'failed to create zip file' ) ) ; } } else { throw ( new \ Exception ( 'No data found!' ) ) ; } }
Converts a file or directory into a zip file for consumption by the browser
@param string $path The path to the file or directory
@return string
Converts a file or directory into a zip file for consumption by the browser