简体   繁体   English

如何在Laravel(主设置)和Lumen(sudomain设置)中共享存储目录中的文件

[英]How to share the files in storage directory in a laravel(main setup) & lumen(sudomain setup)

I am working on a laravel+lumen based project. 我正在做一个基于laravel + lumen的项目。 I am making the main setup which will be web based in Laravel 5.2 and the subdomain (api.domain.com) for handling api requests from mobile devices. 我正在进行主要设置,它将基于Laravel 5.2和基于子域(api.domain.com)的Web来处理来自移动设备的api请求。 THe users will be uploading lot of files(mostly images) from both mobile application and web. 用户将同时从移动应用程序和Web上传大量文件(主要是图像)。 And they will be stored in the storage folder which will not be visible publicly. 它们将存储在不会公开显示的存储文件夹中。 But then there are two problems. 但是,这有两个问题。

  1. How do i share the storage folder between both the setups? 如何在两个设置之间共享存储文件夹? I am thinking of keeping the files on a separate server to solve this problem. 我正在考虑将文件保留在单独的服务器上以解决此问题。
  2. Since the storage folder will not be publicly visible how do i retrieve the file? 由于存储文件夹不会公开显示,我该如何检索文件?

You can have a special route that reads and returns the image. 您可以使用特殊的路由来读取和返回图像。 For example a simple closure route like this: There are two approaches depending on the privacy of your files you can use any one of below. 例如,像这样的简单关闭路径:根据文件的隐私,可以使用以下两种方法中的两种方法。

1) Using dedicated controller to serve images: 1)使用专用控制器提供图像:
Controller: 控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;


class FileController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public static function getFile($fileName){
        $path = storage_path() . '/' . $filename;

        if(File::exists($path)){
            return Response::download($path);
        }
        else
            abort(404);
    }
}

Route 路线

Route::get('/file/{file_name}','FileController@getFile');

Now you can access a file called let's say abc.jpg from this url /file/abc.jpg 现在您可以从此url /file/abc.jpg访问一个名为abc.jpg的文件

2) Using symbolic link 2)使用符号链接
If your files does not require any authentication or any processing this method is best and fasr with respect to Controller 如果您的文件不需要任何身份验证或任何处理,则此方法最好,并且相对于Controller来说比较方便
Create a symbolic link between a subfolder in your storage directory and public directory. 在存储目录和公共目录中的子文件夹之间创建符号链接。
In linux you can use following command: 在linux中,您可以使用以下命令:

ln -s /path_to_storage_directory/file /path_to_public_directory/files

In Windows use following command: 在Windows中,使用以下命令:

mklink /j /path_to_storage_directory/file /path_to_public_directory/files

In Windows if you don't want to use CMD this is a standalone application to create symlinks : Symlinker 在Windows中,如果您不想使用CMD,则这是创建符号链接的独立应用程序: Symlinker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM