Solved

php download header - Open With or Save As dialog

Posted on 2008-10-30
3
2,955 Views
Last Modified: 2013-12-13
I have a working PHP script (details below) which correctly downloads a PDF file to the browser. However, I want the browser (Firefox for the moment) to prompt Open With or Save As. Currently, it just prompts me with 'Enter name of file to save to.'

How do I force the prompt?

//from www.zubrag.com/scripts/download.php
// remove some bad chars
$asfname = str_replace(array('"',"'",'\\','/'), '', $_GET['fc']);
if ($asfname === '') $asfname = $title; //filename

// set headers

ob_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"{$title}\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
ob_end_flush();

readfile($myFile);
exit;
0
Comment
Question by:light-blue
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22854159
In the code snippet is a little script that I use to cause a download.  I am on FF 3.0.3.  When I run this script, I get a dialog box that gives me the choice to save the file or select a program to open the file.

HTH, ~Ray
<?php // RAY_force_download
 

function force_download($filename) {

	header("Content-Type: application/force-download");

	header("Content-Disposition: attachment; filename=\"$filename\"");

	header("Content-length: ".(string)(filesize($filename)));

	header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");

	header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

	header("Cache-Control: no-cache, must-revalidate");

	header("Pragma: no-cache");

	echo file_get_contents($filename);

}
 

force_download("RAY_force_download.php");
 

?>

Open in new window

0
 
LVL 1

Author Comment

by:light-blue
ID: 22854186
Hi Ray, I'm beginning to think this is not a PHP isssue since your script and mine work the same way on my setup. When I open open NON pdf files, it works as expected. In internet explorer, I get the proper dialog box too. I suspect this is related to my install of Adobe Acrobat 8 Pro. What happens with PDF's on your end with your code?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22854215
I don't use my code for PDFs - I just put in a regular HTML anchor tag link.  If I left-click the PDF opens in the browser window (or a new window if <a target="_blank") and if I right-click I get the usual choices.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I recently found myself in a Corporate Situation where the client had requested blocking access to any and all websites except his own Domain? Easy? I am sure this would be your answer but their requirement was, this has to be done without using…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now