Send file to browser problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Send file to browser problem

Post 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?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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'>";
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post 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..
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

volka: im only interested in IE6 or better =) any soultions for that?
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

hrm... that would work... but i have to submit a username and password for security =/
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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.
Post Reply