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
Solved

php download header - Open With or Save As dialog

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

I had to do a bit of research to find the answer to this question so I thought I'd share my results.  Due to our outdated mainframe systems, we need to downgrade IE9 to IE8 in order to stay compatible.  We also needed to downgrade Java.  In order to…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

861 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