Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Storage SFTP and uploaded files permissions

I'm using Storage:SFTP (league/flysystem-sftp) to upload some files to an external server. Everything goes fine with a small issue: the files are uploaded with a 0644 (-rw-r--r--) permission. I've tried to use 'public' option on the put method as the example from docs, like

Storage::disk('remote-sftp')->put($filename, $contents, 'public');

but if fails returning FALSE and doesn't uploads the file.

If I remove the 'public' parameter, everything goes well but with the wrong permissions for file.

Is there any way to set the uploaded file permissions to something like 0666?

like image 872
Carlos Mora Avatar asked Mar 28 '19 10:03

Carlos Mora


People also ask

How do I give storage permission in laravel?

Change all file permissions to 644. Change all folder permissions to 755. For storage and bootstrap cache (special folders used by laravel for creating and executing files, not available from outside) set permission to 777, for anything inside.

How do I upload files to laravel directly into storage folder?

Another way to store the uploaded file is to make use of Storage::putfile() method. To make use of the Storage Facade class you need to include the following class: use Illuminate\Support\Facades\Storage; The putfile() method will store the file given in the folder mentioned.

How do I upload files in laravel?

Create File Upload Controller Create a file uploading controller; we define the business logic for uploading and storing files in Laravel. Execute the command to create the controller. Open app/Http/Controllers/FileUpload. php file, and we need to define the two methods to handle the file upload.


1 Answers

Finally the solution was a combination of Alpy's answer and configuration. Calling setVisibility() went without failure, but keep permissions in 0644. Digging into the FTP/SFTP driver found that the 'public' permission has a pattern that can be assigned in config using 'permPublic' key, so writting in config/filesystems.php the desired octal permission it worked as spected.

  'disks' => [

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

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

    'remote-sftp' => [
        'driver' => 'sftp',
        'host' => '222.222.222.222',
        'username' => 'myuser',
        'password' => 'mypassword',
        'visibility' => 'public',
        'permPublic' => 0766, /// <- this one did the trick
// 'port' => 22,
        'root' => '/home',
// 'timeout' => 30,
    ],

],

];

like image 97
Carlos Mora Avatar answered Sep 22 '22 10:09

Carlos Mora