GhostCyborg GhostCyborg - 3 months ago 9
SQL Question

Stop Header From Downloading File in PHP

I seem to have a problem in my PHP code, whenever I access the file in my browser, it automatically downloads the file, when it should instead display the file. Here's my code:

<?php
$fileID = $_GET['f'];
require("GetMime.php");
$con = mysql_connect("localhost","root","password");
if (!$con){
die("Coulnd't connect to serer: ". mysql_error());
}
mysql_select_db("DownloadCenter",$con);
$FileID = mysql_query("SELECT FileSRC FROM FileCenter WHERE FileKEY = '$fileID'");
$result = mysql_fetch_array($FileID);
$FMime = checkMime($result['FileSRC']);
if (file_exists($result['FileSRC'])){
$contents = file_get_contents($result['FileSRC']);
header('Content-type: '. $FMime);
echo $contents;
}
?>

Answer

Like you discovered the headers aren't being set because output had already started earlier in the php file.

On the third line you have require("GetMime.php");

The checkMime function in GetMime.php has echo finfo_file($finfo, $fileDES);

You call checkMime before you set the header. You can't echo anything out before setting the headers.

Comments