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.";
}
?>

without reading a line of code, but reading your problem, I'm going to take a stab in the dark and say your database field type is set to an ASCII character type, not a binary type. Which would produce the result you described. Which means you need to change the field type and re-insert the data. If this isn't the case, sorry. :)

without reading a line of code, but reading your problem, I'm going to take a stab in the dark and say your database field type is set to an ASCII character type, not a binary type. Which would produce the result you described. Which means you need to change the field type and re-insert the data. If this isn't the case, sorry. :)

Hi, Kireol! Thanks for your post.

Nope, the MySQL field type for all files is meduimblob, so there's something else going on here. Any other ideas?

Thanks again,
Shoshana

i'm not sure. if it were me, I'd write the file with file_put_contents() and then see if my browser can open it. if it can, then it's the HTML part of the code, if it can't, it's the database side. and go from there

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.