Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File creation and download on click 1

Status
Not open for further replies.

Dweezel

Technical User
Feb 12, 2004
428
GB
I'm setting up an interface where a user can select a start date and end date and when a button is clicked it will download all of the data stored in a database table between those two dates as a text file.

I could use some advice on this. I'm guessing that retrieving the data and then writing it to a file on the server is the first step. Would I need to use 'header' functions to get the file to download automatically?

The data retrieval and file creation isn't a problem, but I could use some help with the format of the header functions (if that is indeed what's needed).

Also, is destroying the file that's been created using code placed after header()'s feasible?

Any help appreciated.
 
as an example:

Code:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

I got this expample here.

so in your case it should be a txt file, so I got this example "from the same page!"

Example said:
I had a special situation where I wanted to have a "Download" button on a PHP script, and have the the script post to itself to call a special block of code to initiate download of dynamically created content (rather than display contents of a file).

I was running in circles trying to get IE SP1, SP2 and Firefox on Win98, 2000, and XP to all work where the download prompt is displayed AND the filename is controlled by me and not the browser. I found the following headers from a form that seem to work in all cases.:
Code:
<?php

// load content into var

$filecontent="Some text/code im creating in the script";
$downloadfile="somefile.txt";

header("Content-disposition: attachment; filename=$downloadfile");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".strlen($filecontent));
header("Pragma: no-cache");
header("Expires: 0");

?>

Chacal, Inc.[wavey]
 
That's fantastic Chacal,just what I needed. I had read the header function section of the manual, but I didn't read through all the user comments (that'll teach me).

Thanks a lot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top