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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂