【问题标题】:PHP Download only working for files with text in itPHP 下载仅适用于其中包含文本的文件
【发布时间】:2016-04-09 10:05:36
【问题描述】:

我希望能够使用 php 从我的服务器下载文件。到目前为止它工作得很好,但仅适用于其中包含文本的文件(.txt.php,因此包含简单文本的文件(即使我有一个有趣的现象,在文本开始之前总是有一个空行......想法为什么?),但是当我尝试下载.jpg 文件或.exe 时,它根本不起作用(尝试打开时出错...)

这是我使用的代码:

<?php

session_start();

$file = basename($_GET['file']);

$path = 'uploads/'.$_SESSION['userid']."/".$file;
?>

<?php
if(!file_exists($path)){
    die("file not found");
} else {
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename="'.$file.'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($path);
    exit;
}

【问题讨论】:

  • 如何调用这个脚本?我认为您的服务器通过 mime 类型为 php 和 txt 文件设置了标头。
  • 这是另一个 php 文件中的链接,如下所示:echo "&lt;a href='download.php?file=".$entry."'&gt;".$entry."&lt;/a&gt;\n".'&lt;br/&gt;';

标签: php file file-io download


【解决方案1】:

文件中有一个空行的原因只是因为您的代码中有一个空行

...
$path = 'uploads/'.$_SESSION['userid']."/".$file;
?>
                       <--- There's the empty line.
<?php
if(!file_exists($path)){
    die("file not found");
...

解决方案是将两个 PHP 块合并为一个,而不是两个单独的块。

这也会破坏非文本文件,因为它们实际上会将空行解释为数据并尝试处理它。

【讨论】:

  • 太棒了,这真的有效...但是你能解释一下为什么吗?为什么code和我下载的文件内容有关系
  • @user5638730 基本上 PHP 只会处理 &lt;?php ?&gt; 标签内的东西。这些标签之外的任何内容都将被写出(就像您有echoed 一样)并成为正在下载的文件的一部分。当标签之间有空格时,这与文本或 HTML 一样适用。
  • 所以如果我在两行之间写“hello”(你的评论在哪里),我可以在我的文本文件中读到 hello?
  • @user5638730 是的,完全正确。
  • 有意思,以前没有想过,可能就是下载的时候那么显眼。
【解决方案2】:

问题解决了,

我现在更改了我的脚本以手动响应不同类型的文件,就像这样:

<?php

session_start();

$filename = basename($_GET['file']);
$filename = 'uploads/'.$_SESSION['userid']."/".$filename;

$file_extension = strtolower(substr(strrchr($filename,"."),1));

switch ($file_extension) {
    case "pdf": $ctype="application/pdf"; break;
    case "exe": $ctype="application/octet-stream"; break;
    case "zip": $ctype="application/zip"; break;
    case "doc": $ctype="application/msword"; break;
    case "xls": $ctype="application/vnd.ms-excel"; break;
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpe": case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default: $ctype="application/force-download";
}

if (!file_exists($filename)) {
    die("NO FILE HERE");
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$filename") or die("File not found.");

【讨论】:

    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 2016-04-20
    • 2014-12-09
    相关资源
    最近更新 更多