dsy19890123 2013-01-23 23:02
浏览 45

导出为CSV PHP作为下载文件

I am using a CSV file to export a mysql table. How can I view it as a download file now that it is stored on Drive C: directly without any notification

<?php
$host = 'localhost'; // <--  db address
$user = 'root'; // <-- db user name
$pass = 'root'; // <-- password
$db = 'urs'; // db's name
$table = 'veiwresult'; // table you want to export
$file = 'alaa'; // csv name.
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query(" SELECT ApplicantNum,name, averg,choice FROM veiwresult");
fputcsv($f, array('ApplicantNum','name','averg', 'choice'));
$timestamp = date('Ymd-His'); 

$f = fopen("C:/mycsv-{$timestamp}.csv", 'w');
// Headers    
while($row = mysql_fetch_row($result))
{
fputcsv($f, $row);
}


fputcsv($f, $items_array);
fclose($f);

?>
  • 写回答

2条回答 默认 最新

  • doubingqi5829 2013-01-25 16:29
    关注

    If you want to store csv-file and then open it without any clicking, it`s impossible.

    If you just wish to have it opened, open it in browser window via

    header('Content-disposition: inline;filename=foobar.csv');
    header('Content-Type: text/csv;charset=UTF-8');
    echo $csv_content;
    

    where $csv_content is a string with contents of csv-file. You cat get it this way

    $csv_content = file_get_contents('c:/mycsv.csv');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。