0

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;
}
?>
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
Mateo
  • 71
  • 1
  • 3
  • 11
  • generally, browsers will display the file if they know how to do so. if you serve up something they can't render, they'll offer to save it. so what mime type ARE you serving up? – Marc B Aug 31 '16 at 21:06
  • FYI: Here's the content of GetMime.php: http://pastebin.com/40D7iMzb. and that's what checkMime.php is for, it should check the file and detect the mime, and then set the mime in the header. For example, i'm using a PHP file, but instead of displaying it, it just downloads it instead – Mateo Aug 31 '16 at 21:06
  • your calling a url, on a working server? not just asking for the local file? –  Aug 31 '16 at 21:07
  • A file directory in the same server, e.g, "/home/user/file.pdf – Mateo Aug 31 '16 at 21:09
  • well does your browser have something to display *.pdf's –  Aug 31 '16 at 21:10
  • Not part of the question, but probably worth mentioning: The script does not do any input sanitation on `$_GET['f']` / `$fileID` and uses `$fileID` directly in the SQL query, which opens up the possibility to do [SQL injections](https://en.wikipedia.org/wiki/SQL_injection). – Striezel Aug 31 '16 at 21:11
  • It's a test server, and will never be open to the public. – Mateo Aug 31 '16 at 21:15
  • try thses headers: `header("Content-type:application/pdf"); header("Content-Disposition: inline; filename=$fileName"); // inline makes the browser dispaly` –  Aug 31 '16 at 21:19
  • Just checked the file on localhost, I seem to be getting this error " Cannot modify header information - headers already sent by (output started at C:\WampServer\www\TestFile.php:4) in C:\WampServer\www\TestFile.php on line 14") – Mateo Aug 31 '16 at 21:19
  • the plot thickens, so whats the output on line 4? and you need to stop it. –  Aug 31 '16 at 21:31
  • Somehow fixed it doing this: http://pastebin.com/nsqTWuNE, also, don't quite understand what you mean nogad – Mateo Aug 31 '16 at 21:34

1 Answers1

1

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.

Evan Taylor
  • 216
  • 3
  • 9