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!

Dynamic file creation problem 1

Status
Not open for further replies.

Dweezel

Technical User
Feb 12, 2004
428
GB
I posted a question at the weekend regarding the creation of downloadable data files on click. I was looking for a means to gather data when a user clicks a submit button, put the data into a string, and then have the data downloaded to the user as a file by forcing the browser to lauch the file download dialog using header functions.

Chacalinc posted a reply which quoted from one of the many user comments in the PHP Manual:

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");

?>

I've used the exact same code in my script, even down to the variable names. All that happens when I click the form submit button that launches the script is get a wait of about 10-15 seconds followed by the download of a completely empty file.

When I remove all the header tags and replace it with an echo, all of my code prints out perfectly, so there's nothing wrong with the $filecontent datastring. I've also made sure that nothing is echoing to the browser before the header tags.

As a test I used the following code on it's own in a php script:
Code:
<?php                                            
$filecontent="I can't get this to work";
$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");
?>

I had exactly the same result with this. A rather long wait followed by the download of an empty file.

I'm pulling my hair out with this one.

Can anyoune help?



 
Here are the headers that I use for an export routine in a web application I am working on. I have changed the variables to match your example. Hope this is what you need:
Code:
<?php                                            
$filecontent="I can't get this to work";
$downloadfile="somefile.txt";
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding: none'); 
header('Content-Type: application/octetstream; name="' . $downloadfile . '"');
header('Content-Type: application/octet-stream; name="' . $downloadfile . '"');
header('Content-Disposition: attachment; filename="' . $downloadfile . '"'); 
echo $filecontent;
exit;
?>
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Fantastic Jeff. Just what I needed.

Many thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top