【发布时间】:2017-09-06 21:52:30
【问题描述】:
在我的应用程序中,我需要:
- 上传文件
- 在数据库中存储信息
- 将文件存储在本地或远程文件系统中
- 列出所有数据库行以及下载文件的链接
- 从数据库和文件系统中删除文件
我正在尝试开发第 4 个,但找到的解决方案 here 和 here 对我不起作用。
我的 filesystem.php 是:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
'myftpsite' => [
'driver' => 'ftp',
'host' => 'myhost',
'username' => 'ftpuser,
'password' => 'ftppwd',
// Optional FTP Settings...
// 'port' => 21,
'root' => '/WRK/FILE/TEST',
// 'passive' => true,
// 'ssl' => true,
// 'timeout' => 30,
],
在Controller 中,我将文件存储为:
... validation here ...
$path = $request->uploadfile->storeAs('', $request->uploadfile->getClientOriginalName(), self::STORAGEDISK);
$file = new TESTFile;
... db code here ...
$file->save();
此时我想检索变量以传递给下载方法(我的文件的 url 或路径)。我找到了两种方法
Storage::url($pspfile->filename) *return* **/storage/** accept.pngStorage::disk(self::STORAGEDISK)->getDriver()->getAdapter()->applyPathPrefix($pspfile->filename) *return* C:\xampp\htdocs\myLaravel\ **storage** \app\accept.png
非常感谢您以更好的方式提供帮助或建议。
编辑
目前我将本地/公共与 FTP 分开。
如果在Controller我修改了,下载就可以了
$path = $request->uploadfile->storeAs('',
$request->uploadfile->getClientOriginalName()
,self::STORAGEDISK);
$file->fullpath = $path;
与
$file->fullpath = storage_path('app\\') . $path;
其中 'app\' 是 filesystem.php 中配置为 root 的 storage_path 此外,我可以避免硬编码和使用
$file->fullpath = Storage::disk(self::STORAGEDISK)
->getDriver()
->getAdapter()
->getPathPrefix() . $path;
这样下载方式就可以使用了
return response()->download($pspfile->fullpath);
我仍在寻找一种方法来检索 img 标记的有效 scr 属性。
此外,我希望远程存储的文件也一样(可能是本地临时目录和文件?)
【问题讨论】: