Re: PHP/Mysql: Making binary field available for download - extra browser window ?

From: Date: Fri, 02 Jun 2006 12:01:32 +0000
Subject: Re: PHP/Mysql: Making binary field available for download - extra browser window ?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-237254@lists.php.net to get a copy of this message
Matthew Pegg wrote:
Hi all, Working on a PHP script at the moment where I want to be able to extract a file (PDF file) from a mysql database and force the popup of the File Download dialog, to allow the user to either save or open the document (ie. and bypass loading the file using the browser's plugin) I've found solutions on the net for doing this with a file using readfile() but if I can get away with it, I don't want to create temporary files...
not having temporary files and even the code you use have nothing to do with the extra window problem - that is a client side issue over which php has no control .. it's down to the browser settings (possibly) or (more likely) the target attribute of the link in question.
Here's what I've got so far (truncated...):
    <?
    ..
    // db query string extract the PDF file
    $result = mysql_query("select file_data from brochure where id=1",$connection);
    $row    = mysql_fetch_array($result);
    $lengths = mysql_fetch_lengths($result);
    // Grab the file contents as returned from the db
    $pdf_brochure = $row["file_data"];
    // setup headers to force download of $pdf_brochure into crap.pdf
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=\"crap.pdf\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . $lengths[0]);
    // send the file to stdout
    print $pdf_brochure;        // <---- problem might be here....     exit();
    ?>
This almost works but the problem is that this code still spawns a new empty browser window, and I don't know how to prevent this.... Can anyone help ?
don't open a new window to download it then ... i.e. don't use 'target="_blank"' in the html defintion of the relevant link.
regards, Matt.


« previous php.general (#237254) next »