3

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.

Jhorra
  • 6,233
  • 21
  • 69
  • 123
  • 1
    Try closing the file after writing out to it.. Does that help? – UltraInstinct Jun 15 '12 at 18:12
  • Is the file created? Also, you should be exiting the script after `readfile()`. – nickb Jun 15 '12 at 18:13
  • Why not just generate a link to the downloadable content on the same page as the display? That way they can view the report uninterrupted and choose if they want to download it rather than first deciding if they want to download it before they view it. Proactive vs. reactive. This would solve your headers issue. – aowie1 Jun 15 '12 at 19:30
  • Where would the generate link point to? I don't want to actually create the file, the only other option I could see is creating a page that can handle any of the reports, and I was hoping to keep the reports separate. – Jhorra Jun 15 '12 at 21:36

1 Answers1

8

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(..)
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104