I have written an application to do the following

  • upload a txt file to the server
  • move it from the temporary directory to a more permanent one
  • parse the test file line by line and do some processes
  • create and write the result to a new text file

But the challenge now is to initiate a download sending the written/newly created file back to the user. I don't want to create a page with a link, just the commands required to start the download to the user as if they had clicked on a link.

Can anyone give me an idea about how to do this? Searching for something so seemingly simple has not produced the results I'm looking for.

The script that parses and creates the new text file is before any header or HTML sent to the browser, so header redirection is possible. Thus far, I tried this:

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$dumpf."\";"); 
//variable $dumpf is the name of the file with rewritten data

The file I wish to download to the user is in the same directory as the PHP script, so I know it is not a file path issue.

The file referred to as $dumpf is txt with a series of MySQL queries. The file I get back bears the correct file name but only contains

<html>
</html>

Thanks for taking the time to read this.

my code for forced download is very like yours,
dont want the pdf to open in the browser

if(!$file) return false;
header("Content-disposition: attachment; filename=$file");
header('Content-type: application/pdf;');
readfile("$file");

try,

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;  filename=$dumpf\;");
readfile("$dumpf");

Worked great! Thanks. It's just what I was looking for. Have a Great Day. I know I will. :)

my code for forced download is very like yours,
dont want the pdf to open in the browser

if(!$file) return false;
header("Content-disposition: attachment; filename=$file");
header('Content-type: application/pdf;');
readfile("$file");

try,

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;  filename=$dumpf\;");
readfile("$dumpf");
This question has already been answered. Start a new discussion instead.