2

I can't seem to figure this out and I know it's something simple. I am building the back-end to a very basic content management system. For this specific piece, I am just trying to create a PHP link that allows for a file (the client's CV) to be downloaded.

MY PROBLEM:

When the link to download the file is clicked, instead of the browser prompting you to choose a local directory to save the file to - it simply displays the file and a bunch of symbols before and after the document's contents (I am assuming this is the file's opening and closing exif data for an application to decipher).

How could I go about forcing the browser to prompt the user for a "Save As..." box?

<?php
require("connect.php");
$query = "SELECT * FROM kb_info LIMIT 1";

$result = mysql_query($query, $link);
while ($row = mysql_fetch_array($result)) {
    $file_extension = end(explode(".", $row["extension"]));

    if ($file_extension == doc) {

        header('Content-disposition: attachment; filename='.$row["extension"]);
        header('Content-type: application/doc');
        header ("Content-Length: ".filesize($row["extension"]));
        readfile($row["extension"]);
        exit;

    }
    if ($file_extension == docx) {

        header('Content-disposition: attachment; filename='.$row["extension"]);
        header('Content-type: application/docx');
        header ("Content-Length: ".filesize($row["extension"]));
        readfile($row["extension"]);
        exit;

    }
    if ($file_extension == pdf) {

        header('Content-disposition: attachment; filename='.$row["extension"]);
        header('Content-type: application/pdf');
        header ("Content-Length: ".filesize($row["extension"]));
        readfile($row["extension"]);
        exit;

    }
}    
?>  

Many thanks,

Joshie

j0sh1e
  • 296
  • 4
  • 11
  • I'm also pretty new to PHP, if you have any other mark-up suggestions let me know! Thanks – j0sh1e Feb 27 '13 at 22:34
  • Couple things - you might try a `CASE` switch instead of multiple `IF` statements. Also, the file extension should be in quotes on the `if ($file_extension == pdf)` parts. – Quentin Skousen Feb 27 '13 at 22:43
  • @kkhugs I added the `CASE`, but for the quotes around the extension do you mean around the variable that it is checking? or around the static value that the `IF` is checking against? – j0sh1e Feb 28 '13 at 01:51

2 Answers2

1

I think the problem can be that there is some whitespace somewhere in the PHP files, which causes that the headers are not sent correctly and therefore you see the whole output.

I would suggest the followings steps:

  1. check the "connect.php" and look for empty lines/spaces at the begining/ending of the file and remove them

  2. adapt you php files that way, that you leave out the ending tag ?> at the end of the file - that way you do not get empty lines at the end of the file

  3. if the above are not enough you need to check your apache and php error log and/or set up error loging, so you see also warnings - that you you would be informed if the headers are not sent correctly or if there is some other error

Asped
  • 3,083
  • 4
  • 31
  • 52
0

Headers I use for download:

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-type: application/force-download");
        header("Content-Disposition: attachment; filename=".$file); 
        header("Content-Type: application/octet-stream");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".$bytes."");
Wiggler Jtag
  • 669
  • 8
  • 23
  • Even with those headers, it still displays the doc + exif within the browser and doesn't prompt for actual download. – j0sh1e Feb 28 '13 at 01:50
  • What webserver do you use? Are headers sent correctly? (try to add at the top of script ob_start();) .... for me this script works, just tested it; try it without your mysql code, just make the script as simple as possible to get whats the problem. – Wiggler Jtag Feb 28 '13 at 17:06