Solved

php download header - Open With or Save As dialog

Posted on 2008-10-30
3
2,977 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

738 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