private function convert_to_csv($input_array, $output_file_name, $delimiter) {
$temp_memory = fopen('php://memory','w');
foreach ($input_array as $line) {
fputcsv($temp_memory, $line, $delimiter);
}
fseek($temp_memory, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
fpassthru($temp_memory);
}
我使用上面的函数获取数据数组,转换为 CSV,然后输出到浏览器。两个问题:
- 通过 HTTP 下载后文件是否会从内存中删除?
- 如何重写这个相同的函数,以便可以使用该文件(例如,用作使用 PHPMailer 发送的电子邮件附件),然后立即从内存中删除?
编辑:工作代码 - 但写入文件,而不是内存
public function emailCSVTest() {
$test_array = array(array('Stuff','Yep'),array('More Stuff','Yep yep'));
$temp_file = '/tmp/temptest.csv';
$this->convertToCSV($test_array, $temp_file);
$this->sendUserEmail('Test Subject','Test Message','[email protected] /cdn-cgi/l/email-protection',$temp_file);
unlink($temp_file);
}
private function convertToCSV($input_array, $output_file) {
$temp_file = fopen($output_file,'w');
foreach ($input_array as $line) {
fputcsv($temp_file, $line, ',');
}
fclose($temp_file);
}
仍然没有答案:原始函数是否从内存中删除文件?