Click here to Skip to main content
15,888,301 members
Articles / Programming Languages / PHP

Create & Download Zip File Using PHP

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
20 Oct 2014CPOL2 min read 57.6K   9  
How to create and download a zip file using PHP

If you are trying to download multiple files at the same time and downloading the files one by one, then there is a better option to create a zip file, add the downloading files in the zip and download the zip file. Using PHP, this is very easy. PHP ZipArchive() class provides all functionality to create a zip file with multiple files. You need PHP 5.2.0 and higher for that class.

Create Zip File

  • First thing you have to fix a path and a name for the zip file. The zip file will create in that location with the name, so choose a suitable location in your project folder.
    $zip_file = '(path_of_the_zip)/filename.zip';
    If you are working on WordPress custom plugin, then select the path in your custom plugin folder.
    $dir = plugin_dir_path( __FILE__ );
    	$zip_file = $dir . '/filename.zip';
  • Now create the Zip Archive and open the zip file. If the $zip->open() does not return true, then there is some problem, so exit with some message
    $zip = new ZipArchive();
    	if ( $zip->open($zip_file, ZipArchive::CREATE) !== TRUE) {
    	exit("message");
    	}
  • Add files in the Zip file. To add any file, we can add the file using the file path or we can add the file using its contents.
    $zip->addFile('full_path_of_the_file', 'custom_file_name');

    1st parameter is the full path of the file with the file name and file extension.

    2nd parameter is for file name inside the zip file, if it supplied then the original file name will be overridden.

    Now add file using its contents.

    $download_file = file_get_contents( $file_url );
    	$zip->addFromString(basename($file_url),$download_file);
  • Now don’t forget to close the zip file.
    $zip->close();

Download the Created Zip File

Now the file is ready for download. You know the created xip file location and name so you can add a tag for download the zip file or you can force download using PHP for automatic download.

  • For force download, you have to set the header correctly. When you are setting the header for download the Zip file that time <coce>Content-type and Content-Disposition correctly. In Content-Disposition, add the attachment, it will suggest the browser to download the file instead of displaying it directly.
    header('Content-type: application/zip');
    	header('Content-Disposition: attachment; filename="'.basename($zip_file).'"');
    	header("Content-length: " . filesize($zip_file));
    	header("Pragma: no-cache");
    	header("Expires: 0");
  • Now the header is set, so clear the output buffer before downloading the Zip file. Sometimes, the download Zip file does not open correctly and in Safari, the auto unzipping also failed for some reason. To avoid the problem, you need to clean the output buffer.
    ob_clean();
    	flush();
  • Now download the Zip file in your local.
    readfile($zip_file);
  • The Zip file location and name is always the same, so how many times the functionality executes every time the new file will be added in the same Zip file, so keeping the created Zip file after download complete is just a waste of disk space, so delete the Zip file after download.
    unlink($zip_file);
    If you need to keep the created Zip file all the time, that time doesn’t use unlink(), just change the Zip file name every time, such that add the time with the file name.
  • Last but not the least, don’t forget to exit after readfile().
    exit;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --