Solved

php download header - Open With or Save As dialog

Posted on 2008-10-30
3
2,984 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

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 110

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

691 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