首页 > 解决方案 > 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 文件。我想知道这是否正常,如果不是,我该如何解决。

标签: phplaravel

解决方案


您每次都在创建文件file_put_contents。您可以使用:

Response::download($fileText)->deleteFileAfterSend(true);

或者

return response()->streamDownload(function () use($content) {
    echo $content;
}, 'file.php');

在第二种方法中,删除该file_put_contents


推荐阅读