dpoxk64080 2013-09-18 13:56
浏览 42
已采纳

如何上传使用php创建的csv文件?

I had write this code but it only generate csv file but not upload in directory.

$file = 'export';
    $csv_output .= "CustID,CustName,CustMobile,CustEmail
";

$sql = "select CustID,CustName,CustMobile,CustEmail from dep_customer_details LIMIT 0,10";
$res = mysql_query($sql);
while ($row = mysql_fetch_row($res)) {
 for ($j=0;$j<$res;$j++) {
  $csv_output .= $row[$j].", ";
 }
 $csv_output .= "
";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;


$uploaddir = dirname(__FILE__) .'/csv/';
$uploadfile = $uploaddir.$filename;
move_uploaded_file($filename,$uploadfile);
  • 写回答

2条回答 默认 最新

  • doufei8250 2013-09-18 14:56
    关注

    This code will create a file with export_2013-09-18_10-41.csv for example (as per time of execution on my end) which saves it inside a sub-folder called csv.

    However this is only an example using Mark Baker's comment file_put_contents($uploadfile,$csv_output); and not using headers.

    That can be implemented afterwards as an attachment. You will need to find a function for it yourself.

    <?php
    
    $file = 'export';
    $csv_output = "CustID,CustName,CustMobile,CustEmail
    ";
    
    // Commented out your DB code for you to test 
    // my example with above $csv_output fields only.
    /*
    $sql = "select CustID,CustName,CustMobile,CustEmail from dep_customer_details LIMIT 0,10";
    $res = mysql_query($sql);
    while ($row = mysql_fetch_row($res)) {
     for ($j=0;$j<$res;$j++) {
      $csv_output .= $row[$j].", ";
     }
     $csv_output .= "
    ";
    }
    */
    
    $filename = $file."_".date("Y-m-d_H-i",time());
    
    $uploaddir = dirname(__FILE__) .'/csv/';
    $uploadfile = $uploaddir.$filename.".".csv;
    
    file_put_contents($uploadfile,$csv_output);
    
    print $csv_output;
    
    // Mail function and headers for file attachment
    // can be implemented in this section afterwards
    
    ?>
    

    As Mark Baker also stated, "As the file hasn't been uploaded from a web browser but is being generated by your script on the server...", move_uploaded_file() is usually executed using a form along with other directives in order to move a created file into a folder/sub-folder.

    Therefore, the use of file_put_contents is more suitable for this type of application because it will save to file and not upload, which is not what you need.

    More information on both functions can be found on the PHP.net Website.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?