Page 1 of 1

Send file to browser problem

Posted: Mon Oct 13, 2003 2:01 pm
by liljester
ok... here is what i want to do.

i run a query on a database. i create a txt file with the query results. now i want that txt file to be sent to the browser, so the person that runs the report can save it to their local drive.

im trying it via headers, and here is what i have so far:

Code: Select all

<?php
	$output_file = fopen("tmp_report.txt", "wb");
	fwrite($output_file, $output_txt);
	fclose($output_file);
		
	$download_size = filesize("tmp_report.txt");
	header("Content-type: application/x-download");
	header("Content-Disposition: attachment; filename=tmp_report.txt;");
	header("Accept-Ranges: bytes");
	header("Content-Length: $download_size");
	@readfile("tmp_report.txt");
	exit;
?>
this doesnt quite act the way i want it to... once the file is downloaded, i want the page to refresh... but it doesnt, it just sits there hehe. any ideas?

Posted: Mon Oct 13, 2003 2:09 pm
by DuFF
add this at the end in order to refresh:

Code: Select all

<?php
echo "<meta http-equiv='refresh' content='1; url=$PHP_SELF'>";
?>

Posted: Mon Oct 13, 2003 2:51 pm
by volka
no, this would only append <meta http-equiv='refresh' content='1; url=.... to the downloaded document (type: application/x-download).
I've never found a solution that worked for all browser and proxies but if there is I'd be glad to hear/read about it ;)

Posted: Mon Oct 13, 2003 2:56 pm
by liljester
DuFF wrote:add this at the end in order to refresh:

Code: Select all

<?php
echo "<meta http-equiv='refresh' content='1; url=$PHP_SELF'>";
?>
if i do that, the script will ignore it (because of the exit statement). if i put it before the exit statement, it will be added to the end of the file..

Posted: Mon Oct 13, 2003 2:57 pm
by liljester
volka: im only interested in IE6 or better =) any soultions for that?

Posted: Mon Oct 13, 2003 3:07 pm
by Stoneguard
What I had to do was redesign my concept so that they clicked on a link which in effect called the page and returned the download, that way no browesr window was opened and no blank screen was left up.

So my links look something like this:

Code: Select all

<a href=fileDownload.php?VersionId=3&RevisionId=0><img src=/imap/images/download.gif border=0 alt='Download file'></a>
The actual fileDownload.php runs through some code then returns a downloaded file without opening a browser window or forwarding on to a blank page (In IE 6).

However, I tried a similar call in javascript using 'window.location=' and it opened a new browser window. So I am not 100% sure how to automate this at this point.

Posted: Mon Oct 13, 2003 3:41 pm
by liljester
hrm... that would work... but i have to submit a username and password for security =/

Posted: Mon Oct 13, 2003 3:50 pm
by Stoneguard
I use session variables to store the username/password. Then I check them in the call to filedownload.php. Not sure if that would work for your situation though.