Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

I have a directory at that need to zip archive. JavaScript


I have a directory at that need to zip archive. I used as a model but it doesn't work.

Everything with php is up to snuff:

<li><a href="/zip-folder.php" target="_blank">zip events</a></li>

file: zip-folder.php
// report, but don't show errors (will need to check php log)
error_reporting( E_ALL );
ini_set( 'display_errors', 0 );
ini_set( 'display_startup_errors', 0 );

// options
$zipdir = "cccr-data";
$zipname = "";
$zip = new ZipArchive();
$count = 0;

// check if folder can be found
if ( !is_dir( $zipdir ) ) {
http_response_code( 500 );
die( "Could not find folder to zip for download (".$zipdir.")." );

// try to open the output file
if ( $zip->open( $zipname, ZIPARCHIVE::CREATE ) !== true ) {
http_response_code( 500 );
die( "Could not could not create zip file for download (".$zipname.")." );

// get list of all files to be added to zip (recursive)
$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $zipdir ) );

foreach ( $files as $path ) {
$path = ltrim( str_replace( "\\", "/", $path ), "\\/." );
$fpath = str_replace( "\\", "/", realpath( $path ) );

if ( is_dir( $path ) ) { continue; }
// echo "<pre>".$path." | ".$fpath."</pre> <br /> \n";
$zip->addFile( $fpath, $path ) && $count++;

// check if the file was created and items added to it
if ( !$count || !file_exists( $zipname ) ) {
http_response_code( 500 );
die( "There was a problem zipping the file for download (".$zipname.")." );

// looks good, send as download response
http_response_code( 200 );
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$zipname."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize( $zipname ) );
@readfile( $zipname );
@unlink( $zipname );

Answers (3)


Rainner Lins answers:

Hey, try this:

Put that code in a PHP file on your server, make sure to update the options to point to the folder relative to where you put this script on your server. This will zip the folder and send it as a download, just need to link to it from your site, no JS needed.

For example:

<li><a href="/zip-folder.php" target="_blank">zip events</a></li>

User180435 comments:

My file structure is as follows:

at home: localhost/www/verlager, and localhost/www/verlager/cccr-data/ contains the files to be zipped.
on web:, contains the files to be zipped.

<li><a href="./zip-folder.php" target="_blank">zip events</a></li>

// options
$zipdir = "./cccr-data";
$zipname = "./";


Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

User180435 comments:

at home in LAMP the error message is:

There was a problem zipping the file for download (./

Rainner Lins comments:

Try leaving the slash out of the name for the zip file, try just , and check the permissions on both the php file and the directory it's in, as it needs to write a file, so it needs write permission.


Monit jadhav answers:

The implementation is wrong my friend

You are trying to execute PHP functions using javascript which is not possible. You can do it with Ajax but you will have to create a PHP file with the implementation on

It will look something like this
<li onclick="zipfolders();">zip events</li>

Include jquery in your file and use a script like below to zip folder

$( document ).ready(function() {
function zipfolders() {
url: 'scripts/zipFolders.php',
type: 'POST',
success: function ()
alert("Zipped Successfully!");

The PHP code that zips folders goes into zipFolders.php and if the function returns true the folder will be zipped.

Let me know if you have any doubts I can help you solve this.


User180435 comments:

I updated the code above to demonstrate my compliance with your instructions. Still doesn't work.

Monit jadhav comments:

Ok I will try it out myself and let you know



Arnav Joy answers:

are you still looking for help?