2017-04-11 62 views
0

在我的應用程序有必要:Laravel無法下載

  1. 上傳文件在DB
  2. 存儲在本地或遠程文件系統
  3. 該文件列出了所有
  4. 存儲信息帶有鏈接的db行下載文件
  5. 從db中刪除文件並從文件系統中刪除文件

我正在嘗試開發第4個解決方案,但找到的解決方案herehere不適用於我。

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或路徑)。我發現2種方式

  • Storage::url($pspfile->filename) *return* **/storage/** accept.png
  • Storage::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; 

其中'應用\'storage_pathfilesystem.php 配置爲此外,我可以避免硬編碼和使用

$file->fullpath = Storage::disk(self::STORAGEDISK) ->getDriver() ->getAdapter() ->getPathPrefix() . $path;

這樣的下載方法可以使用

return response()->download($pspfile->fullpath); 

我仍然在尋找一種方法以檢索的IMG標籤的有效SCR屬性。

另外我想和遠程存儲的文件相同的(也許與當地的臨時目錄和文件?)

回答

0

我提出了類似前一段時間的東西。也許這個例子代碼可以幫助你。

class FileController extends Controller 
{ 
    // ... other functions ... 

    public function download(File $file) 
    { 
     if (Storage::disk('public')->exists($file->path)) { 
      return response()->download(public_path('storage/' . $file->path), $file->name); 
     } else { 
      return back(); 
     } 
    } 

    public function upload() 
    { 
     $this->validate(request(), [ 
      'file-upload' => 'required|file', 
     ]); 

     $path = request()->file('file-upload')->store('uploads', 'public'); 
     $file = new File; 
     $file->name = request()->file('file-upload')->getClientOriginalName(); 
     $file->path = $path; 
     $file->save(); 

     return back(); 
    } 
} 
+0

**上傳**而不是使用商店我使用了storeAs。而不是** public **我從本地切換到FTP(僅用於基本測試)。正如你所看到的,爲了存儲在filesystem.php中配置的遠程目錄中或爲了將文件存儲在存儲/應用程序中的本地磁盤,我留空了_directory name_。在兩種方式中,文件按預期存儲。存儲在數據庫中的路徑與文件名相同。 _Storage_ facade的_get_方法也可以工作,但我不明白是否需要它下載,或者我是否只需要path()和如果可以使用相同的路徑作爲img標記中的src attrib。 – AlexMI