Jump to content


Stream File Download


  • Please log in to reply
3 replies to this topic

#1 _*otto_*

_*otto_*
  • Guests

Posted 09 September 2004 - 09:30 AM

Is there a tutorial that can teach me how to force a download with the file contents that's comes from a variable. The variable has sql results.

I found tutorials that use files that are located on the hard drive, but I'm trying to stream it from variable.

#2 _*otto_*

_*otto_*
  • Guests

Posted 09 September 2004 - 10:27 AM

** Solved **

#3 _*mYth_*

_*mYth_*
  • Guests

Posted 09 September 2004 - 01:54 PM

Now if you kindly will, tell us how?

#4 _*otto_*

_*otto_*
  • Guests

Posted 09 September 2004 - 02:26 PM

This is what I came up with.
Just make sure that you fill in your own database info.

<?
//////////////////////////////////////////////
//
//  Exporting Database as CSV
//
//
//	This can be used with anything.
//	I made this so that you can export database
//	results to a CSV file.
//	You can modify it to send anything.
//
//////////////////////////////////////////////


$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
$table = "";

// This is for line returns
$client = $_SERVER["HTTP_USER_AGENT"];
if(ereg('[^(]*\((.*)\)[^)]*',$client,$regs)) 
{
$os = $regs[1];
	// this looks better under WinX
	if (eregi("Win",$os)) 
    $crlf="\r\n";
}
// Connect to database
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Cound not connect");
mysql_select_db($dbname, $link) or die ("Couldn't select database");
// DB QUERY
$result = mysql_query("SELECT * FROM $table");

// File that shows up on clients computer
// Since this is an octetstream, use .txt or .csv
$filename = "file.csv";
// Set header options
header("Content-disposition: filename=$filename");
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");

while($row = mysql_fetch_array($result))
{
// Put what ever you want to show up in the file here
// The 'username' in $row["username"] is the field name.
// You must use print, not echo.
// i.e. 
// print "blah blah blah";
// At the end of each line put $crlf for the line return.
// i.e.
// print "blah blah blah $crlf";
print $row["id"].",".$row["username"].",".$row["password"]."$crlf";
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users