public function zipFile($source, $destination, $includeFolder = false) {
        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));
        $folder = $includeFolder ? basename($source) . '/' : '';

        if (is_dir($source) === true) {
            // add file to prevent empty archive error on download
            $zip->addFromString('fm.txt', "This archive has been generated by Rich Filemanager : https://github.com/servocoder/RichFilemanager/");

            $files = new RecursiveIteratorIterator(
                    new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST
            );

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

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

        return $zip->close();
    }
public function zipFile($source, $destination, $includeFolder = false) {
        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));
        $folder = $includeFolder ? basename($source) . '/' : '';

        if (is_dir($source) === true) {
            // add file to prevent empty archive error on download
            $zip->addFromString('fm.txt', "This archive has been generated by Rich Filemanager : https://github.com/servocoder/RichFilemanager/");

            $files = new RecursiveIteratorIterator(
                    new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST
            );

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

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

        return $zip->close();
    }
public function zipFile ( $ source , $ destination , $ includeFolder = false ) { 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 ) ) ; $ folder = $ includeFolder ? basename ( $ source ) . '/' : '' ; if ( is_dir ( $ source ) === true ) { // add file to prevent empty archive error on download $ zip -> addFromString ( 'fm.txt' , "This archive has been generated by Rich Filemanager : https://github.com/servocoder/RichFilemanager/" ) ; $ files = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( $ source , RecursiveDirectoryIterator :: SKIP_DOTS ) , RecursiveIteratorIterator :: SELF_FIRST ) ; foreach ( $ files as $ file ) { $ file = str_replace ( '\\' , '/' , realpath ( $ file ) ) ; if ( is_dir ( $ file ) === true ) { $ path = str_replace ( $ source . '/' , '' , $ file . '/' ) ; $ zip -> addEmptyDir ( $ folder . $ path ) ; } else if ( is_file ( $ file ) === true ) { $ path = str_replace ( $ source . '/' , '' , $ file ) ; $ zip -> addFile ( $ file , $ folder . $ path ) ; } } } else if ( is_file ( $ source ) === true ) { $ zip -> addFile ( $ source , $ folder . basename ( $ source ) ) ; } return $ zip -> close ( ) ; }
Creates a zip file from source to destination
@param  	string $source Source path for zip
@param  	string $destination Destination path for zip
@param  	boolean $includeFolder If true includes the source folder also
@return 	boolean
@link	http://stackoverflow.com/questions/17584869/zip-main-folder-with-sub-folder-inside
Creates a zip file from source to destination