【问题标题】:Database to CSV(Done) trying to Save file to folder on server数据库到 CSV(完成)尝试将文件保存到服务器上的文件夹
【发布时间】:2013-06-18 10:10:54
【问题描述】:

我已经成功地将我的数据库导出为 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");

【问题讨论】:

    标签: php csv save export


    【解决方案1】:

    为什么要写入流,读取它而不是尝试保存内容? 我会以更小的方式来做:

    //Open a file in write-mode (he creates it, if it not exists)
    $fp = fopen('./my/path/on/server/data.csv', 'w');
    
    // output the column headings
    fputcsv($fp, 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($fp, $row);
    //close the handler
    fclose($fp); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      相关资源
      最近更新 更多