...

View Full Version : Send data as file



Goldfish
03-11-2010, 11:14 PM
Iīm trying to send a user a page, where I want to send part of the page as a file, like an attachment to an e-mail.

Something like



$query = mysql_query("SELECT * FROM `someData`");
while($row = mysql_fetch_object($query)) {
if($SomeCondition) {
print $row->data;
} else {
$putinfile = $putinfile.$row->data;
}
}

// here I want to send $putinfile as a file to the user



I have no clue how I could do that, but I believe there should be a way other than fwrite() it into a file and print a link into the browser?

I donīt mind if the user gets the normal download dialog displayed, where he can choose where he wants to save the file, all I need is a way to send data as file, without having an actual file on the server.

Could anyone give me a hint?
Thanks in advance.

mlseim
03-11-2010, 11:29 PM
What is the filetype?
Is $putinfile a filename or some text ... or what is it?

kbluhm
03-11-2010, 11:32 PM
Untested but should do the trick:


header( 'Content-type: application/octet-stream' );
header( 'Content-length: ' . strlen( $putinfile ) );
header( 'Content-disposition: attachment; filename="whateveryouwant.txt"' );

echo $putinfile;

exit;


Or, using PHP's output stream (just for good measure):


header( 'Content-type: application/octet-stream' );
header( 'Content-length: ' . strlen( $putinfile ) );
header( 'Content-disposition: attachment; filename="whateveryouwant.txt"' );

if ( FALSE !== ( $fp = fopen( 'php://output', 'w' ) ) )
{
fwrite( $fp, $putinfile );
fclose();
}

exit;

The output streaming would be good if you potentially have a LOT of row data to output, and instead of gobbing up a ton of memory in one fell swoop, you could stream it to the client as it is retrieved.

Goldfish
03-11-2010, 11:41 PM
I have to try that.
Thanks

Edit:
The good news: It works, the user receives the file.
The bad news: The browser doesnīt print the rest of the page, but remains on the previous page.

So how can I print the page the browser should display after the file has been sent, without the user has to click somewhere?



$temp = "xyz";
header( 'Content-type: application/octet-stream' );
header( 'Content-length: ' . strlen( $temp ) );
header( 'Content-disposition: attachment; filename="whatever.txt"' );
echo $temp;

print $RestofPage

mlseim
03-12-2010, 12:02 AM
Can you show us the URL so we can see how it flows?

Goldfish
03-12-2010, 12:14 AM
There is no URL (yet)

All I do so far is checking out how it would work, before I wrap a complete script around it, meaning Iīm reducing it to the core function, which in this case is, send a file plus print a page.

You may imagine an empty Website, where the index.php contains nothing but this:



<?php
$temp = "xyz";
header( 'Content-type: application/octet-stream' );
header( 'Content-length: ' . strlen( $temp ) );
header( 'Content-disposition: attachment; filename="whatever.txt"' );
echo $temp;
?>
<html>
<head><title>Test</title></head>
<body>
<p>
Test
</p>
</body></html>


This will successfully send a file named "whatever.txt" with the content "xyz" to the user, but unfortunately it wonīt write the HTML part to the users browser.

kbluhm
03-12-2010, 12:17 AM
No, the content must be streamed from it's own file in this case, with no output afterward. I alluded to that by placing exit() at the end of each example. ;)

Goldfish
03-12-2010, 12:26 AM
Ah well, I figured that by now, so I guess I have to work around it.
Thanks anyway, very fast, very helpful.

mlseim
03-12-2010, 12:28 AM
So what's the purpose of "the attachment".
Is there a reason the output couldn't be displayed on the page that is being displayed?
So instead of the header, you have a PHP "include" on the page they are viewing?

I can't figure-out what the purpose is ... what you're doing.

Goldfish
03-12-2010, 12:46 AM
The purpose is a complex calculation of tons of numbers, where I want to display a summary of the result in the users browser, while at same time sending him a detailed version as file.
The reason why I donīt display both in the browser is, the file can easily reach 50 pages of text, while the summary fits into 20 lines.

Side note: Thanks again to kbluhm for the streaming version, I really can use that one.

As an example, imagine you want to create statistics about visitors on your homepage.

The summary would show:
The site has x members.
In last x days y users have logged in.
The most users online were x at date/time.

The detailed file then shows something like:

User name1 login date/time
clicked x pages
stayed y minutes
caused z KB traffic

User name 2 login date/time

etc etc

kbluhm
03-12-2010, 01:12 AM
No problem, make sure you place the fwrite() within your result-fetching while-loop, like so:


$query = mysql_query( 'SELECT `data` FROM `table`' );

if ( FALSE !== ( $fp = fopen( 'php://output', 'w' ) ) )
{

while ( $row = mysql_fetch_object( $query ) )
{
fwrite( $fp, $row->data );
}

fclose();

}

mysql_free_result( $query );

Also, with this method you will be unable to accurately set the Content-length header... so just leave it out and keep the client on it's toes over how much downloading is left to go. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum