php 生成一个下载文件
<?php
// 文件名
$filename='example.txt';
// 文件内容
$content ='这是一个示例文件。';
// 打开文件句柄
$handle = fopen($filename, "w');
// 写入文件内容
fwrite($handle,$content);
// 关闭文件句柄
fclose($handle);
// 下载文件
header('Content-Type: application/octet-stream');
header('Content-Disposition:attachment; filename="”.$filename .'"');
header('Content-Length:".filesize($filename));
readfile($filename);
// 删除文件
unlink($filename);
?>在上面的示例中,我们首先指定要生成的文件名和文件内容。然后,我们打开一个文件句柄,并使用 fwrite() 函数将内容写入文件。接下来,我们关闭文件句柄,并使用 header() 函数将文件作为下载提供给用户。最后,我们使用 unlink() 函数删除生成的文件。
请注意,上面的示例中,我们使用 Content-Type: application/octet-stream 头来指定文件类型。这是因为我们不知道文件的确切类型。如果您知道文件类型,请使用适当的 MIME 类型。