使用php创建和下载文本文件 [英] Create and download a text file using php

查看:136
本文介绍了使用php创建和下载文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要做的事情。我有一系列报告,他们也希望能够以逗号分隔的文本文件下载。我已经阅读了一堆页面,人们说只是回声结果,而不是创建一个文件,但是当我尝试它输出到他们所在的页面。



我以每份报告的形式出现

 导出文件< ; input type =checkboxname =exportvalue =1/> 

所以在发布时,我可以检查他们是否尝试导出文件。如果他们正在尝试这样做:

  if($ _ POST ['export'] =='1')
{
$ filename = date(DOB教练 - $ month)。 '。文本';

$ content =;

#CSV文件
$ content =名称,地址,城市,州,邮政编码,DOB \; ($ i = 0; $ i< count($ instructors); $ i ++)


$ content。=; //填写内容

fwrite($ filename,$ content);

header(Cache-Control:public);
header(Content-Description:File Transfer);
header(Content-Length:。filesize($ filename)。;);
header(Content-Disposition:attachment; filename = $ filename);
header(Content-Type:application / octet-stream;);
header(Content-Transfer-Encoding:binary);

readfile($ filename);
}

基本上刷新页面,但没有文件被推送下载。任何人都可以指出我失踪了吗?



编辑
我想我还不完全清楚。这不在一个只创建和下载文件的页面上,这个页面也显示在该页面上。所以当我把一个exit(); readfile后,页面的其余部分加载空白。我也需要在此页面上显示报告。我认为这也可能与为什么不下载,因为这个页面已经发送标题信息。

解决方案

我忽略了在您要求您尝试关闭文件之前,您正在写出内容的方式。



在此查看fwrite手册: http://php.net/manual/en/function.fwrite.php



您需要做的是:

  $ filename =yourfile.txt; 
#...
$ f = fopen($ filename,'w');
fwrite($ f,$ content);
fclose($ f);

,关闭文件后,您现在可以安全地将其发送到下载。

  header(Cache-Control:public); 
header(Content-Description:File Transfer);
header(Content-Length:。filesize($ filename)。;);
header(Content-Disposition:attachment; filename = $ filename);
header(Content-Type:application / octet-stream;);
header(Content-Transfer-Encoding:binary);

readfile($ filename);

有几件事:




  • 你真的不需要将内容类型设置为 application / octet-stream 。为什么不设置一个更真实的类型为 text / plain

  • 我真的不明白你想如何使用日期功能。请参阅手册: http://php.net/manual/en/function。 date.php

  • 正如@nickb所正确指出的那样,在执行 readfile(..)


Here's what I'm trying to do. I have a series of reports that they also want to be able to download as comma delimited text files. I've read over a bunch of pages where people say simply echo out the results rather than creating a file, but when I try that it just outputs to the page they are on.

I have this in the form of each report

Export File<input type="checkbox" name="export" value="1" />

So on the post I can check if they are trying to export the file. If they are I was trying to do this:

if($_POST['export'] == '1')
{
    $filename = date("Instructors by DOB - ".$month) . '.txt';

    $content = "";

    # Titlte of the CSV
    $content = "Name,Address,City,State,Zip,DOB\n";

    for($i=0;$i<count($instructors);$i++)
        $content .= ""; //fill content

    fwrite($filename, $content);

    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Length: ". filesize("$filename").";");
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Type: application/octet-stream; "); 
    header("Content-Transfer-Encoding: binary");

    readfile($filename);
}

Basically the page refreshes, but no file is pushed for download. Can anyone point out what I'm missing?

EDIT I think I wasn't entirely clear. This is not on a page that only creates and downloads the file, this is on a page that is also displaying the report. So when I put an exit(); after the readfile the rest of the page loads blank. I need to display the report on this page as well. I think this could also have to do with why it's not download, because this page has already sent header information.

解决方案

I overlooked the way you are writing out the contents before asking you to try closing the file.

Check the fwrite manual here: http://php.net/manual/en/function.fwrite.php

What you need to do is:

$filename = "yourfile.txt";
#...
$f = fopen($filename, 'w');
fwrite($f, $content);
fclose($f);

and after closing the file, you can now safely send it across for download.

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary");

readfile($filename);

There are a couple of things:

  • You really dont need to set the content-type as application/octet-stream. Why not set a more real type as text/plain?
  • I really dont understand how you want to use the date functionality. please refer to the manual here: http://php.net/manual/en/function.date.php
  • As correctly pointed out by @nickb, you must exit the script after doing the readfile(..)

这篇关于使用php创建和下载文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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