0

I'm struggling with a simple php script that combine files into a single .zip and then start downloading it.

My script is called through AJAX with Wordpress.

The response header seems fine but the response is a large amount of weird characters (see below).

Probably a problem with the zip file encoding but I can't figure it out.

Note that I can find the zip on my server.

The response headers

enter image description here

The php function

function zipFilesAndDownload($file_names,$archive_file_name) {
ob_start();
$zip = new ZipArchive();
$archive_link = WP_CONTENT_DIR . '/uploads/' . $archive_file_name;

// Create the zip file and throw error if problem
if ($zip->open($archive_link , ZIPARCHIVE::CREATE )!==TRUE) {
    exit("Cannot create <$archive_file_name>\n");
}

foreach($file_names as $files)
{
    // Create the absolute path for the files
    $file_full_path =  WP_CONTENT_DIR . '/uploads/' . $files;
    // Clean the file name to remove date path created by WP (/2014/05/file -> file)
    $file = end((explode('/', $files)));

    // Check if the file exists
    if ( file_exists( $file_full_path ) ){

        // Add the file to the zip with the new $file name
        $zip->addFile( $file_full_path, $file );

    } else {
        exit("ERROR file doesn't exist : $file\n");
    }
}

$zip->close();

if(file_exists($archive_link)){
    // Send the proper header to download the zip
    header('Content-Description: File Transfer');
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="'.basename($archive_link).'"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($archive_link));
    ob_end_clean();
    flush();
    readfile($archive_link);
    //unlink($archive_link);
    exit;
} else {
    exit("ERROR can't find <$archive_file_name>\n");
}

}

The weird characters

enter image description here

tshepang
  • 12,111
  • 21
  • 91
  • 136
Nypam
  • 1,458
  • 3
  • 11
  • 25
  • 2
    The zip format is binary, not readable text. What exactly did you expect? What's the problem? If you open a zip file with a dumb text editor you will get the same weirdness (or worse), but that doesn't mean there is a problem. – Jon May 20 '14 at 11:17
  • 1
    The contents of zip are binary, do a force download rather reading the contents into the AJAX request for file download – rsakhale May 20 '14 at 11:20
  • @rsakhale Thank for pointing me in the right direction... It was so obvious. – Nypam May 20 '14 at 12:36

1 Answers1

0

I just echoed an array containing the download link :

echo json_encode(array("link"=>$downloadLink));

and within my ajax success callback function :

$.post(ajax_object.ajax_url, data, function (response) {
    $("body").append("<iframe src='" + response.link + "' style='display: none;' ></iframe>");
}, "json");

This post helps me : https://stackoverflow.com/a/8394118/345901

Community
  • 1
  • 1
Nypam
  • 1,458
  • 3
  • 11
  • 25