Solved

php download header - Open With or Save As dialog

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

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 109

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

810 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