Jump to content

[SOLVED] stream zip


manic

Recommended Posts

Hi, I want to force the download of a zip file, but first I want to run some php script to validate the download. I don't need to create or unpack the zip, just stream it from a php file. I have tried the following script, but all I get is the uncompiled code in the browser:

 

<?
//access validation code goes here

$filename = "test.zip";
$myFile = "/home/site/public_html/test/test.zip";

header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: application/zip");
header("Content-Length: " .(string)(filesize($myFile)) );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary\n");

readfile($myFile);
?>

how can I force the donload pop up for this zip folder?

 

Thanks

Link to comment
Share on other sites

are you outputting anything to the browser before you start your "download" headers?

 

Common "errors" are a blank line at the end of an included file (or the top of your page):

 

  <-- this blank will cause the file download to be weird
<?php

// some php code

?> <-- a space here will throw it off as well, but usually only for included files
  <-- same with a blank line here

 

If you have any echo or print statements, those count to.  As well as anytime you escape and reenter php, e.g.:

 

<?php

//some code

?> <-- this is a "newline" being sent to the browser
<?php

//some more code

?>

 

Some say that just doing "?><?php" in your script, with no space or anything else between them will cause header errors...I haven't tested that though.

 

The reason that you are seeing "uncompiled code" in your browser is because when some form of output is sent to the browser before the headers you are specifying the browser automatically sees it as being "text/html" (or something like that).  Sending different headers ("application/zip") doesn't change that, so the browser continues to interpret the data it's receiving as text.

Link to comment
Share on other sites

hmm. I'm afraid not. I've got <?php on the very first character of the first line, and nothing after the last ?>. There are no php tags anywhere else. This is the only code that I have on the page.

 

I'm wondering if it is something to do with the readfile function - does this automatically send output as text? Is there something else I should be doing in my headers to force download. Perhaps there is a generic method to force the download box for any file type - this would be very useful...

Link to comment
Share on other sites

ooo! I found it!

http://elouai.com/force-download.php

 

<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
  exit;
} elseif ( ! file_exists( $filename ) ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
  exit;
};
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.