Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
815 views
in Technique[技术] by (71.8m points)

php - Force Download Only Displays in Browser, Doesn't Prompt for Download

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...