1

I am working on a laravel+lumen based project. 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. THe users will be uploading lot of files(mostly images) from both mobile application and 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?
Pawan Kumar
  • 594
  • 4
  • 18

1 Answers1

0

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:
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

2) Using symbolic link
If your files does not require any authentication or any processing this method is best and fasr with respect to Controller
Create a symbolic link between a subfolder in your storage directory and public directory.
In linux you can use following command:

ln -s /path_to_storage_directory/file /path_to_public_directory/files

In Windows use following command:

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

jaysingkar
  • 4,315
  • 1
  • 18
  • 26
  • 1
    Sorry I didn't realize that you want to share the files between two setups. In this case, you better use symbolic inks. Just replace path to public directory to your 2nd project's storage directory. – jaysingkar Jul 24 '16 at 21:32