【问题标题】:Why is & being converted to &amp?为什么 & 被转换为 &amp?
【发布时间】:2016-10-17 21:37:21
【问题描述】:
$file="csv.php?task=whovotedwho&election=7"; 
$filename = "whovotedwho-election7.csv"; 

if (file_exists("trash.png")) {
        header('Content-Description: File Transfer');
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="'.($filename).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize("trash.png"));
        readfile($file);
        exit;
    } else { echo "file does not exist"; }

我在下载的 csv 中得到这个结果。

【问题讨论】:

  • 看起来你安装了几个扩展(比如 x-debug),其中一个有错误并执行双 html 编码
  • php 文件是否有 readfile 等效项?
  • 您没有下载 CSV 文件。看看你得到了什么,这是一条错误消息,说readfile()失败。
  • 你为什么使用filesize("trash.png")?这与下载的 CSV 文件的大小有什么关系?
  • 您不能将带有查询字符串参数的 url 传递给 include。你试过readfile("http://www.example.com/csv.php?task=whovotedwho&election=7")

标签: php file csv


【解决方案1】:

这个:

$file="csv.php?task=whovotedwho&election=7"; 

它不是像http://example.com/csv.php... 这样的完整/绝对 url,所以当 readfile() 启动时,它正在执行 LOCAL 文件请求,并寻找名称字面上为 csv.php?tasketc... 的文件。 执行 http 请求 - 它不能。您没有为该 http 请求提供发送到的协议或主机。

PHP 不像您的浏览器,<img src=kittens.jpg> 之类的内容在内部被翻译成 <img src="http://example.com/kittens.jpg">

坦率地说,即使你在那里有一个完整的 URL,这样做也是非常痛苦的——你已经在执行 http 请求的完全相同的服务器上执行代码,所以这就像在你的车里跳并在城市周围行驶 500 英里,这样您就可以将车停在出发地左侧 1 英尺处。

您在错误中看到的是原始 HTML。由于您的文件名中包含 &,因此它必须进行 HTML 编码,以便在输出中显示为文字 &,并且不会呈现为 html 实体。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    相关资源
    最近更新 更多