简体   繁体   English

在ram中写入csv,然后将文件输出给用户[php]

[英]write csv in ram and output file to user [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). 我正在使用PHP使用以下功能创建一个csv文件。

 // 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. 如您所知,它是与TYPO3相关的,但是这里不应该打扰。 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. 因此,我想到了一个主意:为什么在创建该文件之后不只是打开save-file dialog This means, the file shouldn't be stored on the FS, right? 这意味着该文件不应该存储在FS上,对吗?

I read a bit about the output-buffer , but what would the file for the $handle be? 我读了一些有关output-buffer ,但是$handle的文件是什么? PHP:\\\\out? PHP:\\\\出?

Just to clarify: User clicks "export" -> after proccessing the data and finishing the file, show SAVE-dialog in Browser. 需要澄清的是:用户在处理完数据并完成文件后单击“导出”->,在浏览器中显示SAVE对话框。 Don't save the file on the FS. 不要将文件保存在FS上。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM