将生成的CSV以适当的格式保存到服务器作为下载

时间:2013-06-20 08:14:33

标签: php html csv save

我已成功将数据库导出为csv作为可下载文件。然而,我现在需要做的是不是创建一个直接的.csv文件,我需要它只需保存到服务器上名为“csv”的文件夹。这是我当前导出的代码。我需要帮助将其保存到服务器。我没有正确保存数据。

    // output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('tax_class_id','_product_websites'));

// fetch the data
mysql_connect('localhost:3036', 'x', 'x');
mysql_select_db('lato');
$rows = mysql_query('SELECT taxclass,productwebsite FROM product');

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) 
fputcsv($output, $row);

$filename = "data.csv"; // Trying to save file in server
file_put_contents("download/" . $filename, "$header\n$rows");

1 个答案:

答案 0 :(得分:0)

您需要将最后一行更改为

$filename = "data.csv"; // Trying to save file in server
file_put_contents("download/" . $filename, $output);