0

I want to download the zip file in php. Here is my code below.

<?php 
ob_start();
// set example variables
$filename = "test.zip";
$filepath = "/home/somewhere/file/zip";
// http headers for zip downloads
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
readfile($filepath.$filename);
?>

And i have a link in my html code.

<html>
    <head>
    </head>
    <body>
        <a href="myPHPfile.php">Download</a>
    </body>
</html>

The file is downloaded successfully when i clicked the link named 'Donwload' but i can't unzip and open the file.

The file name is test.zip when i downloaded for the first time and it is just the file name. Its extension is created when i click it to unzip. The extension of file is ". cpgz" not the ".zip".

Here is the log when i downloaded it.

Resource interpreted as Document but transferred with MIME type application/zip:"myPHPfile.php"

Did i do something wrong in my code? or miss something? How can i fix this? My uploaded file is already zipped in the server and all i want to do is just download it.

홍의숙
  • 297
  • 4
  • 11
  • 28

2 Answers2

0

The problem is your filepath, there's a missing trailing "/" after "/home/somewhere/file/zip" the working directory should be: $filepath = "/home/somewhere/file/zip/";

Spikey21
  • 431
  • 2
  • 8
0

Now this code works great!

<?php
$filepath = iconv("UTF-8","CP949", "/home/somewhere/zip/test.zip");

header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=test.zip");
header("Content-Transfer-incoding: utf-8");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath));

ob_end_flush();
readfile($filepath);
?>
홍의숙
  • 297
  • 4
  • 11
  • 28