I'm having trouble sending files to a browser from a PHP script. My script is based on an article at http://www.oreillynet.com/pub/a/php/2000/09/15/php_mysql.html. I had to modify the method that the filename is passed to this script as the method in the article simply did not wormk (for me).
Everything is going fine until the point the 'Save File' dialog pop's up in IE5. The name of the file is correct and I have the option to 'Open' or 'Save'. I choose 'Save' and clicky OK.
At this point, the Download dialog displays and indicates it's getting file information. A spilt second later (e.g. real fast), IE5 pop's up an error window indicating it cannot download the file (please try again later). The filename and url in the error msg is correct.
This is PHP4 BTW. Anyone any ideas?
Thanks,
Chris Curran
<?php
include("session.inc");
// This script get's called as 'url/fmdl.php/filename'. On entry
// $PATH_INFO == '/filename'. Get rid of '/' in front of filename.
$parm = substr($PATH_INFO,1,strlen($PATH_INFO)-1);
if ($parm=='') {
header('Location: fmlist.php');
exit;
}
// connect to mysql server
dbConnect();
// get file data from sql
$sql = "SELECT fdata,ftype,fsize FROM filePit WHERE fname='$parm';";
$result = @($sql) or
die($sql);
//load php var's
$data = @mysql_result($result, 0, "fdata");
$size = @mysql_result($result, 0, "fsize");
$type = @mysql_result($result, 0, "ftype");
// set 'if (1)' to 'if (0)' to dump data to browser (debug)
if (1) {
header("Content-length: $size");
header("Content-type: $type");
//header("Content-type: applicationet-stream");
header("Content-Disposition: attachment; filename='$parm'");
header("Content-Description: PHP Generated Data");
}
else {
echo("Content-length: $size<br>");
echo("Content-type: $type<br>");
echo("Content-Disposition: attachment; filename='$parm'<br>");
echo("Content-Description: PHP Generated Data<br>");
}
echo $data;
?>