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'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:
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?
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?