Hi, everyone! I'm new to DaniWeb and also to PHP and MySQL.
I have a script, previewfile.php, that extracts files of various file types (currently .doc, .docx, .xls, .xlsx, and .pdf) from a MySQL database and is supposed to display them to the user. I'm encountering difficulties in displaying the files, both in IE 8 and Firefox 3.5.5; in IE 8, I get the error message, "Internet Explorer cannot download file previewfile.php from <server>". In Firefox 3.5.5, I can download the files, but they show up either empty or with messed-up formatting, as the file types don't appear to be recognized.
As you can see from the excerpt below, I've been trying different things, but to no avail. Any help/advice you could provide would be appreciated!
Thanks in advance,
Shoshana
<?php session_start();
$DBconnection = mysqli_connect("localhost", "<db_name>", "<db_password>","<table_name>");
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"]){
$prep = mysqli_prepare($DBconnection,'SELECT files.FILE_STORE, files.FILE_TYPE FROM files JOIN applications ON files.appid = applications.appid JOIN users ON users.userid=applications.userid WHERE (users.userid=? || "reviewer" = ? ) && files.FILE_ID=?');
mysqli_stmt_bind_param($prep,'sss',$_SESSION['userid'],$_SESSION['level'],$_GET['itemid']);
mysqli_stmt_execute($prep);
mysqli_stmt_store_result($prep);
mysqli_stmt_bind_result($prep, $file, $filetype);
mysqli_stmt_fetch($prep);
session_cache_limiter('no-cache');
header('Content-type:'.$filetype);
header('Content-Type:application-x/force-download');
header('Content-Disposition: attachment; filename="$file"');
header("Content-Transfer-Encoding: binary");
// header('Content-Length: ' . filesize($file)); //
// header("Cache-Control: no-store, no-cache, must-revalidate"); //
header ("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: max_age=0");
header ("Pragma: no-cache");
$file = @fopen($file,"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
// echo $file; //
// readfile($file); //
mysqli_stmt_free_result($prep);
}
else{
echo "You do not have permission to view this.";
}
?>