【发布时间】:2023-03-31 14:10:01
【问题描述】:
我想将整个 php 文件复制到一个 csv 文件中并下载它,但我得到的 demo.csv 文件是空的。为什么我得到一个空文件?
php文件中的数据是按行存储的。
<?php
// output headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="demo.csv"');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
$handle = fopen("caldataprog.php", "r"); //fileurl
$lines = [];
if (($handle = fopen("caldataprog.php", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
$lines[] = $data;
}
fclose($handle);
}
$fp = fopen('demo.csv', 'w');
foreach ($lines as $line) {
fputcsv($fp, $line);
}
fclose($fp);
?>
【问题讨论】: