Magas
Magas

Reputation: 514

Save files with storeAs outside the Storage folder

I have in an API in Laravel that stores files

$request->file("file$i")->storeAs('categories', $nameFile)

As it stands it stores the data correctly in the address

/var/www/html/apiapp/storage/app/public/categories

But I would like to save these files in the folder

/var/archives

Because these files will be accessed by another application

Upvotes: 4

Views: 6179

Answers (2)

Magas
Magas

Reputation: 514

The third argument of storeAs tells on which storage disk the file should be saved

$path = $request->photo->storeAs('images', 'filename.jpg', 's3');

So, create a new disk in config/filesystem.php and point it to /var/archives

'disks' => [

    // ...

    'archive' => [
        'driver' => 'local',
        'root' => '/var/archives',
    ],

    // ...

In the code:

$request->file("file$i")->storeAs('categories', $nameFile, 'archive')

Ensure that the user running the webserver has permission to save to that folder on disk as well.

Another alternative is to share an external storage, such as AWS S3 for example. The configuration for this follows the same idea.

I received this answer on stackoverflow in Portuguese and it worked perfectly

Upvotes: 11

yrv16
yrv16

Reputation: 2275

To save your files to a directory outside the webserver root directory /var/www/html You should configure your webserver. Similar problems:

PHP upload a file to a directory outside the webserver root
Set Apache to allow access for a directory not under document root

You can read this, too: Mapping URLs to Filesystem Locations

Upvotes: 0

Related Questions