Database Reference
In-Depth Information
if ( ! get_param_val ( "download" ))
{
# construct self-referential URL that includes download parameter
$url = $_SERVER [ "PHP_SELF" ] . "?download=1" ;
?>
<html>
<head><title> <?php print ( $title ); ?> </title></head>
<body>
<p>
Select the following link to commence downloading:
<a href=" <?php print ( $url ); ?> ">download</a>
</p>
</body>
</html>
<?php
exit ();
} # end of "if"
# The download parameter was present; retrieve a result set and send
# it to the client as a tab-delimited, newline-terminated document.
# Use a content type of application/octet-stream in an attempt to
# trigger a download response by the browser, and suggest a default
# filename of "result.txt".
$dbh = Cookbook :: connect ();
$stmt = "SELECT * FROM profile" ;
$sth = $dbh -> query ( $stmt );
header ( 'Content-Type: application/octet-stream' );
header ( 'Content-Disposition: attachment; filename="result.txt"' );
while ( $row = $sth -> fetch ( PDO :: FETCH_NUM ))
print ( implode ( " \t " , $row ) . " \n " );
$dbh = NULL ;
?>
download.php uses a get_param_val() function that we haven't covered yet. It deter‐
mines whether that parameter is present. This function is included in the Cook‐
book_Webutils.php file and discussed further in Recipe 20.5 .
Another way to produce downloadable content is to generate the query result, write it
to a file on the server side, compress it, and send the result to the browser. The browser
likely will download it and run some kind of uncompress utility to recover the
original file.
Search WWH ::




Custom Search