Jump to content

Force Download script issue


bsamson

Recommended Posts

  Alight. I have a 'forced download' script that I have made my own ... Here's how it works ...

I have all my .pdf files located in the docs directory. So ...  I have links on the Documents page that look like this: http://internal.mydomain.com/scripts/fd-pdf.php?f=3. The 3 is the name of the file. All of the documents in the 'docs' directory are pdf files. So I have the following script:

fd-pdf.php
[code]<?php
$fn = $_GET[f].".pdf";
$fullname = "http://internal.mydomain.com/docs/$fn";

header("Content-Length: " . filesize($fullname));
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=".$fullname);

readfile($fullname);

// echo $fullname;

?>[/code]

  I have echoed the $fullname and I know that it is correct. BUT when I click the link IE tries to download fd-pdf. Can I please get some guidance. Thanks!
Link to comment
https://forums.phpfreaks.com/topic/29534-force-download-script-issue/
Share on other sites

Add these lines to your script:

[code]<?php

if(ini_get('zlib.output_compression')) //Required for IE
  ini_set('zlib.output_compression', 'Off');
header("Pragma: public"); //Some browsers require this
header("Cache-Control: private",false); //Some browsers require this
header("Content-Transfer-Encoding: binary");

?>[/code]

Orio.
Orio,
  alright new script looks like this:

[code]<?php
$fn = $_GET[f].".pdf";
$fullname = "http://internal.mydomain.com/docs/$fn";

if(ini_get('zlib.output_compression')) //Required for IE
  ini_set('zlib.output_compression', 'Off');
header("Pragma: public"); //Some browsers require this
header("Cache-Control: private",false); //Some browsers require this
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($fullname));
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=" . $fullname);

readfile($fullname);

// echo $filename;

?>[/code]

  And still IE tried download the php file, fd-pdf.php. Any suggestions? Could it be this line: header("Content-Disposition: attachment; filename=" . $fullname); ? Thanks!
This is the script I use, it works both for IE and Mozilla. I made a few changes to make it good for your needs:

[code]<?php

$path = "docs/"; //put here the relative path from the script to the files

$filename = $path.$_GET['f'].".pdf";
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$ctype="application/pdf";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
@readfile("$filename");
exit();

?>[/code]

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.