views:

37

answers:

3

Not sure if the title makes sense sorry... basically I'm generating Word documents that I wanted to open automatically on the client's machine, but it seems this isn't possible, so instead of just presenting a list and having them manually click on each one, I was wondering if at the very least it could bring up the 'Would you like to save or open this file' dialogue :(

Long shot, but I know a lot of sites will do this when you download stuff... re-directing to download server etc

Thanks

+1  A: 

You can set the contenttype and content-disposition header to open the open/save dialog box in browser.

See

Header Field Definition

for details.

rahul
+2  A: 

The best you can do is provide a "hint" with the Content-disposition header.

http://php.net/manual/en/function.header.php

Specifying something like

<?php
header('Content-type: application/msword');

header('Content-Disposition: attachment; filename="downloaded.pdf"');

// output content of document
?>

should cause most browsers to prompt the user to download the document, while this:

header('Content-Dispotion: inline')

should usually cause the browser to show the file in the existing window.

meagar
Many thanks - this is kind of perfect, but it uses the current page's content in the .doc file as opposed to just displaying a pre-generated document... hope that makes sense :sCheers
Nick
+1  A: 

You need to send the correct header and so on.

I found this that seems good:

<?php
function send_file($name) {
  ob_end_clean();
  $path = "protected/".$name;
  if (!is_file($path) or connection_status()!=0) return(FALSE);
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");
  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("Content-Type: application/octet-stream");
  header("Content-Length: ".(string)(filesize($path)));
  header("Content-Disposition: inline; filename=$name");
  header("Content-Transfer-Encoding: binary\n");
  if ($file = fopen($path, 'rb')) {
    while(!feof($file) and (connection_status()==0)) {
      print(fread($file, 1024*8));
      flush();
    }
    fclose($file);
  }
  return((connection_status()==0) and !connection_aborted());
}
?>

And here's an example of using the function:

<?php
if (!send_file("platinumdemo.zip")) {
die ("file transfer failed");
// either the file transfer was incomplete
// or the file was not found
} else {
// the download was a success
// log, or do whatever else
}
?>
marcgg