drl6054 2016-03-17 11:27 采纳率: 0%
浏览 25
已采纳

在ram中写入csv并将输出文件写入用户[php]

First of all, I found a similar question, but that doesn't do the trick when if I understand it correctly.

I'm creating a csv-file with PHP with the following function(s).

 // open the file. If not existent yet, create it first
 if (!file_exists($this->filename))
 {
     touch($this->filename);
 }

 $handle = fopen($this->filename, "w+");
 if ($this->includeHeaders)
 {
     $headers = array('Company', 'Contactperson', 'Username', 'Commission', 'WinCAP Version', 'WinCAP Link', 'Contract due', 'POS Version', 'POS Link', 'Quick Select Version', 'Quick Select Link', 'Password');
     fputcsv($handle, $headers);
 }

 // mysql-stuff here

 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
 {
     fputcsv($handle, $row, ";", '"');
 }

 // done
 fclose($handle);

As you can tell, it is TYPO3-related, but that shouldn't bother here. However, my file is generated correctly, containing all the data I need. However, the file is created on the filesystem. That means, anyone with the link to that file, can download it.

This is what I want to prevent. I was thinking about adding a hash, but that doesn't seem to protect it well enough. So I cam up with the idea: why not just open the save-file dialog, after creating that file. This means, the file shouldn't be stored on the FS, right?

I read a bit about the output-buffer, but what would the file for the $handle be? PHP:\\out?

Just to clarify: User clicks "export" -> after proccessing the data and finishing the file, show SAVE-dialog in Browser. Don't save the file on the FS.

  • 写回答

1条回答 默认 最新

  • dongqiang5932 2016-03-17 11:41
    关注

    Just send the file for download and then delete it. Add this right after your code:

    // Send correct http headers
    header('Content-Description: File Transfer');
    header("Content-Type: application/csv") ;
    header("Content-Disposition: attachment; filename=ChooseFilename.csv");
    header("Expires: 0");
    
    // Send file to browser
    readfile($this->filename);
    
    // delete file
    unlink($this->filename);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元