dongxinche1264 2015-06-05 10:55
浏览 68
已采纳

保存CSV文件而不是下载

So I have this code to generate a CSV file from mysql database. But however, it downloads the code instead of saving it to the directory for further use (need to send that file via phpmailer). What changes should I do to make it save the file to the directory.

$array = array();
if(file_exists('records_monthly.csv'))
    {
        unlink('records_monthly.csv');
    }
# Headers
$array[] = array("Serial Number","Donation Type", "Amount", "Status", "Date", "Orderref", "DIN");
$serial=1;
try
{
    $s = $conn->query("SELECT c.firstname AS firstname, c.lastname as lastname, c.address AS address, c.city AS city, c.postalnumber AS postalnumber, c.email AS cemail, d.donation_type as donation_type, d.donation_amount as donation_amount, d.orderref as orderref, d.status as status, d.donation_on AS donation_on, d.din as din from customers c, donations d where c.email = d.donator");
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
while($donations = $s->fetch(PDO::FETCH_OBJ))
{
    if($donations->status == 0)
    {
        $array[] = array($serial++,$donations->donation_type,$donations->donation_amount,"Failed",$donations->donation_on,$donations->orderref,$donations->din);    
    }
    else
    {
        $array[] = array($serial++,$donations->donation_type,$donations->donation_amount,"Success",$donations->donation_on,$donations->orderref,$donations->din);
    }

}

array_to_csv_download($array,"records_monthly.csv",",");

function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
    // open raw memory as file so no temp files needed, you might run out of memory though
    $f = fopen('php://memory', 'w'); 
    // loop over the input array
    foreach ($array as $line) { 
        // generate csv lines from the inner arrays
        fputcsv($f, $line, $delimiter); 
    }
    // rewind the "file" with the csv lines
    fseek($f, 0);
    // tell the browser it's going to be a csv file
 header('Content-Type: application/csv');
    // tell the browser we want to save it instead of displaying it
   header('Content-Disposition: attachement; filename="'.$filename.'";');
    // make php send the generated csv lines to the browser
   fpassthru($f);
}
  • 写回答

1条回答 默认 最新

  • doujiexi1824 2015-06-05 11:04
    关注

    Write directly to the named file rather than to php://memory and don't send headers and output to the browser

    function array_to_csv_without_download($array, $filename = "export.csv", $delimiter=";") {
        $f = fopen($filename, 'w'); 
        // loop over the input array
        foreach ($array as $line) { 
            // generate csv lines from the inner arrays
            fputcsv($f, $line, $delimiter); 
        }
       fclose($f);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。