NYCPHP Meetup

NYPHP.org

[nycphp-talk] Exporting csv

Darian V. Schramm darian at slackworks.com
Tue Sep 21 16:54:45 EDT 2004


Hello List, 

I'm trying to use the header() php function to output a csv from the results
of a mysql query. There was a post on here not long ago about forcing downloads
of a file on the filesystem. Is it possible to force a download of a string? 

Here's my code:

<?
include ('includes/configure.php');
include ('includes/classes/db_utils.php');

function outOfStock(){

$query = "select sku_code, products_quantity, quantity_threshold, products_name, attribute_name from products p join products_description pd on p.products_i
d=pd.products_id where products_status='1' and products_quantity <= quantity_threshold order by products_name";

$rows = DB_UTILS::queryMultiRow($query);
foreach($rows as $row){
    $output .= implode(",",$row);
    $output .= "\n";
}
 return $output;
}


$date = gmdate("m-d-Y",time());
$file = outOfStock();
$filesize = strlen($file);
$filename = "out-of-stock-" . $date;

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size"); 

echo $file;
exit();

?>

It would be possible to write a temp file with the data, but it seems like there 
has to be away to do this.

Thanks in advance,

-
darian <darian at slackworks.com>




More information about the talk mailing list