php - Laravel 5.6:公共文件夹中下载文件的副本
问题描述
每当我访问此功能的路线时:
public function storephp($id)
{
$fileText = "file.php";
$content =
"test content"
file_put_contents($fileText, $content);
return Response::download($fileText);
}
有了这个我可以下载文件,没问题。但是每次我下载文件时,公共文件夹中都会有一个副本。我的意思是,如果我下载了 3 次 file.php 文件,那么 public 文件夹中就有一个 file.php 文件。我想知道这是否正常,如果不是,我该如何解决。
解决方案
您每次都在创建文件file_put_contents
。您可以使用:
Response::download($fileText)->deleteFileAfterSend(true);
或者
return response()->streamDownload(function () use($content) {
echo $content;
}, 'file.php');
在第二种方法中,删除该file_put_contents
行
推荐阅读
- android - AnchorNode/Models 在 android sceneform sdk 中消失了
- terraform - 如何将 LogDNA 或 SysDig 实例标记为平台日志或指标的默认目标?
- sql-server - MS Access VBA将SQL中的字符串解释为int?
- python - Django 创建唯一的外键对象
- x86 - Skylake中干净缓存行的写回?
- java - Flyway:无需迁移
- javascript - 节点脚本无法完成
- python-3.x - 如何使用 python 与控制台应用程序交互?
- javascript - 如何基于 es5/es2015 浏览器支持预取 javascript 文件
- spring-boot - 如何从单元测试中连接@Configuration 文件 - Spring Boot + Kotlin