【问题标题】:PHP Open an existing csv/text file in header pagePHP 在标题页中打开现有的 csv/文本文件
【发布时间】:2012-06-17 14:06:52
【问题描述】:

我可以在页眉中打开现有的文本文件或 csv 文件吗? (即当我将下面的标头参数设置到 php 页面时:

header('Content-Type: application/vnd.ms-excel; charset=UTF-8;');
header('Content-Disposition: attachment;filename='.$document);
header('Cache-Control: max-age=0');

文件将直接在模式警报(打开/保存)下打开。但是如果我有文件名(test.xls),我如何设置标题参数以在标题页中打开它???

假设:一个名为“test.xls”的文件,我想输出一个 php 页面来打开/保存这个文件

$file='/temp/test.xls';// file existed and created previously.

header('Content-Type: application/vnd.ms-excel; charset=UTF-8;');
header('Content-Disposition: attachment;filename='.$file);
header('Cache-Control: max-age=0');

如何进行?

【问题讨论】:

  • 你真正想做什么?重定向并给出一个文件?在页面内容之前打印文件?不清楚,抱歉。
  • 没错,更多细节可能会有所帮助..
  • 我想打开一个现有的文件,比如像 xls 或文本标题这样的弹出警报(打开/保存),通常当我们想要将数据导出到 excel 或文本文件时,我们提取数据并准备尚未创建的文件的 excel 标头或文本,但在这种情况下,文件已创建,我想在页眉上打开它。查看重新编辑的问题

标签: php excel header save


【解决方案1】:
  1. 如果文件是公开可用的,您可以简单地重定向到它:

    header('Location: /path/to/file.xls');
    
  2. 否则,只需发送您已有的标头并输出文件:

    echo file_get_contents('file.xls');
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2020-04-02
    • 2012-08-09
    • 2017-06-15
    • 2010-10-12
    相关资源
    最近更新 更多