laravel 在laravel中下载后删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26295852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting files after download in laravel
提问by vincent
I'm creating a application that lets a user download a file. After the download i want the file to be deleted. The end of my code is like this:
我正在创建一个允许用户下载文件的应用程序。下载后我想删除文件。我的代码的结尾是这样的:
return Response::download(public_path() . '/uploads/_tmp/' . urldecode($filename));
which means that the function ends on the return an i am not able to delete the file. I have tried to call a 'after' filter on the route but then the file gets deleted to quickly.
这意味着函数在返回时结束,我无法删除文件。我试图在路由上调用“之后”过滤器,但随后文件被快速删除。
Any ideas?
有任何想法吗?
回答by Jon
You can use deleteFileAfterSend
http://laravel.com/docs/5.0/responses#other-responses
您可以使用deleteFileAfterSend
http://laravel.com/docs/5.0/responses#other-responses
return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);
return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);
回答by Matt Burrow
I personally use the following;
我个人使用以下;
$response = Response::make(file_get_contents($path_to_file), $status_code, $headers);
Status code is obviously the code which you want to return.
状态码显然是您要返回的代码。
Within the $header parameter you can pass an array with the indexes Content-Type and Content-Disposition.
在 $header 参数中,您可以传递一个带有索引 Content-Type 和 Content-Disposition 的数组。
Then you can simply unlink $path_to_file and return $response.
然后你可以简单地取消链接 $path_to_file 并返回 $response。
Much easier way of deleting a file would be to use Jon's answer for versions of Laravel > 4.0.
删除文件的更简单方法是使用 Jon 对 Laravel > 4.0 版本的回答。
You can use deleteFileAfterSend
http://laravel.com/docs/5.0/responses#other-responses
您可以使用deleteFileAfterSend
http://laravel.com/docs/5.0/responses#other-responses
return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);
回答by juniorb2s
$filename = storage_path() . '/testing.txt';
App::finish(function($request, $response) use ($filename)
{
unlink($filename);
});
return Response::download($filename);
回答by RashedRahat
Simply use this line of code:
只需使用这行代码:
return response()->download($file_path)->deleteFileAfterSend(true);
return response()->download($file_path)->deleteFileAfterSend(true);
Here, inside download function the file path will be passed as an argument. Let's say for an example you want to backup your database in a file and also want to delete with downloading:
在这里,在下载函数中,文件路径将作为参数传递。举个例子,你想在一个文件中备份你的数据库,并且还想通过下载删除:
$date = Carbon::now()->format('Y-m-d_h-i');
$pub_path = public_path();
$file_path = $pub_path . '/application/db_backups/' . $date . '.sql';
$output = shell_exec('mysqldump -h58.84.34.65 -uwsit -pwsit97480 websms > ' . $file_path);
return response()->download($file_path)->deleteFileAfterSend(true);
回答by Jayakarthik Appasamy
unlink('./path/filename.extension');