Jump to content

Download a ZIP


danhodge

Recommended Posts

I have probably brung this up before, but it is slightly driving me crazy...

 

This is my current download PHP, which is working fine - i think someone here gave it me, or i C+P'd...

 

But the point is, whenever i change the type to ZIP and the file to being a ZIP file, it wont work - it downloads, but it tells me i can't open the actual file itself...

 

 

<?php 

$filename = "download.txt"; 

header("Content-Length: " . filesize($filename)); 
header('Content-Type: text/octet-stream'); 
header('Content-Disposition: attachment; filename=download.txt'); 

readfile($filename); 
?>

 

Anyone know why?

 

Thanks,

Danny

Link to comment
Share on other sites

I just tried some sample code from PHP.net (http://php.net/manual/en/function.readfile.php) that I tweaked to work with .zip files, and it's working properly:

 

http://www.falkendev.com/test.php (should download a "test.zip" file that contains a "test.txt" text file.)

 

Here's my code:

 

<?php
$file = 'test.zip';

if (file_exists($file)) {
   header('Content-Description: File Transfer');
   header('Content-Type: application/zip');
   header('Content-Disposition: attachment; filename='.basename($file));
   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($file));
   ob_clean();
   flush();
   readfile($file);
   exit;
}
?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...