仅强制下载仅在浏览器中显示,不提示下载 [英] Force Download Only Displays in Browser, Doesn't Prompt for Download

查看:180
本文介绍了仅强制下载仅在浏览器中显示,不提示下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎不明白这一点,我知道这很简单。我正在构建一个非常基本的内容管理系统的后端。对于这个特定的作品,我只是想创建一个PHP链接,允许下载文件(客户端的简历)。

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.

我的问题: / strong>

MY PROBLEM:

当点击下载文件的链接时,而不是浏览器提示您选择本地目录来保存文件 - 它只是显示文件以及文档内容前后的一串符号(我假设这是文件的开始和关闭exif数据,用于应用程序解密)。

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;

    }
}    
?>  

非常感谢,

Joshie


Joshie

推荐答案

我认为问题可能是在PHP文件中有一些空格,这导致标题不能正确发送,

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.

我建议执行以下步骤:


  1. 检查connect.php并在文件的开始/结束处寻找空行/空格并删除

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

文件,你在文件的末尾省略结束标记?> - 这样你不会在文件的末尾得到空行

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

如果上述不足,您需要检查您的apache和php错误日志和/或设置错误日志,因此您还会看到警告 - 如果标题未正确发送或是否存在其他错误,则会被通知

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

这篇关于仅强制下载仅在浏览器中显示,不提示下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆