我用fputcsv创建了一个文件
$handle = fopen('php://output', 'w+');
// Add the header of the CSV file
fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';');
// Query data from database
// Add the data queried from database
foreach ($results as $result) {
fputcsv(
$handle, // The file pointer
array(...), // The fields
';' // The delimiter
);
}
file_put_contents('mycsv.csv',fgetcsv($handle), FILE_APPEND);
fclose($handle);
....我想将输出保存在mycsv.csv上,但是文件是空的
发布于 2016-10-31 16:29:46
php://output是一个像echo或print一样工作的流。这意味着,您正在编写标准输出(可能是控制台或浏览器)。
如果要将内容写入csv文件,请尝试打开该文件或直接使用file_put_contents创建该文件,而不是使用该。
$handle = fopen("mycsv.csv","wb");
fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';');
//...put your code
rewind($handle);//optional.it will set the file pointer to the begin of the filehttps://stackoverflow.com/questions/40345793
复制相似问题