Tanmay Maity

CREATE & DOWNLOAD ZIP FILE USING PHP

Leave a comment

If you are trying to download multiple files in 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 provide all functionality to create a zip file with multiple file. 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 override.
    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 Zip 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 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 download the Zip file, Some time 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 always same, so how many time the functionality execute every time the new file will be added in the same Zip file, so keeping the created Zip file after download complete is just waste of disk space, so delete the Zip file after download.unlink($zip_file);If you need to keep the created Zip file all time that time don’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;

Leave a comment