Change storage location in laravel 5.4

4

The current storage location for laravel is / laravel / storage / app / public . How do I switch to / laravel / public . I tried editing filesystems.php but I was not successful:

Controller store function:

public function store(Request $request)
{
    $input = $request->all();
    $paciente = new Paciente($input);
    $paciente->save();

    if(empty($request->arquivo))
    {    
        return redirect()->action('PacienteController@index');
    } 
    else
    {
        $file = $request->arquivo;
        $fileName = $file->getClientOriginalName();
        $path = $request->file('arquivo')->storeAs('uploads', $fileName, 'upload');

        $arquivo = new Arquivo();
        $arquivo->paciente_id = $paciente->id;
        $arquivo->nome = $fileName;
        $arquivo->save();

        return redirect()->action('PacienteController@index');
    }   
}

Filesystems.php

return [

/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/

'default' => 'local',

/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/

'cloud' => 's3',

/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "s3", "rackspace"
|
*/

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

    'upload' => [
        'driver' => 'local',
        'root' => public_path(),
        'visibility' => 'public',
    ],

],

];
    
asked by anonymous 17.02.2017 / 17:56

2 answers

5

Create another configuration, example pictures , in your root key use the public_path() " which indicates the public folder of your project:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */

    'default' => 'local',

    /*
    |--------------------------------------------------------------------------
    | Default Cloud Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Many applications store files both locally and in the cloud. For this
    | reason, you may specify a default "cloud" driver here. This driver
    | will be bound as the Cloud disk implementation in the container.
    |
    */

    'cloud' => 's3',

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "s3", "rackspace"
    |
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        'pictures' => [
            'driver' => 'local',
            'root' => public_path(),
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

    ],

];

To save the images use:

$result = $request->file('image');
$ext = $result->getClientOriginalExtension();
$result->storeAs('images', uniqid().'.'.$ext, 'pictures');

or

$result->store('images','pictures'); 

If you do not want to use any of this you can do the traditional filesystems.php that writes directly to the filesystems.php folder (in this example it was placed in the move folder):

$result = $request->file('image');
$ext = $result->getClientOriginalExtension();
$result->move('images', uniqid().'.'.$ext);

18.02.2017 / 01:59
4

You do not need to change anything in your code.

If you are using Linux (or the Homestead en ) just run a command that does a symlink en (symbolic link) between /storage/app/public and /public/storage so you can use disk public without problems:

php artisan storage:link

See this in documentation in .

You can also do symlink on Windows through the command mklink and Laravel already supports this , but this is restricted to admin users on the system.

Fortunately in the next system update, Creators Update this will be simplified en and even non-admin users will be able to do this.

    
18.02.2017 / 02:36