Skip to content

Instantly share code, notes, and snippets.

@umidjons
Created January 31, 2014 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umidjons/8738580 to your computer and use it in GitHub Desktop.
Save umidjons/8738580 to your computer and use it in GitHub Desktop.
Compress file with ZipArchive
<?php
$file_name = 'myfolder/myfile.log';
$info = pathinfo( $file_name );
$zip = new ZipArchive();
$zip_file_name = $info[ 'dirname' ] . DIRECTORY_SEPARATOR . $info[ 'filename' ] . '.zip';
$zip_local_name = $info[ 'basename' ]; // file name in archive without directory
if ( true !== $zip->open( $zip_file_name, ZipArchive::CREATE ) )
die( "Compressing failed!" );
$zip->addFile( $file_name, $zip_local_name );
$zip->close();
// print link to download file:
echo '<a class="my-zip-file" href="<?= $zip_file_name; ?>">Compressed File</a>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment