php - 使用 php 创建、编写和下载 txt 文件
问题描述
$file = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($file, "lorem ipsum");
fclose($file);
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($file));
header("Connection: close");
文件已创建/打开并写入,但未下载。
有什么帮助吗?
解决方案
正确的做法是:
<?php
$file = "test.txt";
$txt = fopen($file, "w") or die("Unable to open file!");
fwrite($txt, "lorem ipsum");
fclose($txt);
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: text/plain");
readfile($file);
?>
推荐阅读
- c - C中的反转计数归并排序
- php - php从字符串创建数组和子数组
- json - 修改 JSON 中的嵌套数组 (Groovy)
- flutter - 在构建构建上下文中使变量运行一次
- html - HTML5 和 CSS 中的框的小问题
- android - 媒体播放器在一段时间后停止并且 Seekbar OnTouch 不工作
- count - 在 Pygame 中只按一次键
- flutter - Flutter FCM onMessage 在听的时候不起作用
- python-3.x - Py4JError:JVM 中不存在 org.apache.spark.api.python.PythonUtils.getPythonAuthSocketTimeout
- r - 在ggplot R中平滑锯齿状时间序列的最佳方法