Devbizz
Devbizz

Reputation: 81

Laravel upload file outside root path

I tried everything but cannot get a file upload to work.

I want it to upload to:

/var/www/mysite.com/uploads

Laravel is located at:

/var/www/mysite.com/admin/public/

Latest thing I tried was making a filesystem like this:

'uploads' => [
    'driver' => 'local',
    'root' => '/var/www/mysite.com/uploads'
]

I also tried

'uploads' => [
    'driver' => 'local',
    'root' => '../../uploads'
]

none of them did work.

Can anyone please tell me how I can upload files, outside of my Laravel directory to the directory I specified above?

EDIT: The error I receive:

League \ Flysystem \ Exception Impossible to create the root directory "/var/www/mysite.com/uploads/image.jpg".

Also tried this:

'uploads' => [
    'driver' => 'local',
    'root' => dirname(dirname(dirname(__FILE__))).'/uploads'
]

with controller code:

$path = $request->file('image')->store(
    'image.jpg', 'uploads'
);

Upvotes: 2

Views: 4688

Answers (2)

zahra_oveyedzade
zahra_oveyedzade

Reputation: 1220

For me the problem is solved in this way, I've created a directory called uploads one level upper than laravel root directory. after that in config/filesystem.php added this piece of code:

'links' => [
        public_path('storage') => storage_path('app/public'),
        public_path('storage2') =>  __DIR__ . '/../../uploads',
    ],

and also,

    'disks' => [

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

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

        'public2' => [
            'driver' => 'local',
            'root' => __DIR__ . '/../../uploads',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        ],

    ],

and finally entered the php artisan storage:link in terminal.


Actually what it does is that in public directory, a directory named storage2 is created and this directory will point to uploads folder that is created outside of laravel root directory.

please note that this lines of code create the uploads direcotry and after that all images will store in this location.

'public2' => [
            'driver' => 'local',
            'root' => __DIR__ . '/../../uploads',
            'visibility' => 'public',
        ],

for storing uploaded image in created uploads directory, I used this code:

        if ($request->hasFile('cover')) {

            $filenameWithExt = $request->file('cover')->getClientOriginalName ();
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            $extension = $request->file('cover')->getClientOriginalExtension();
            $fileNameToStore = $filename . '_' . uniqid() . '.' . $extension;       

            $path = $request->file('cover')->storeAs('/image/blog', $fileNameToStore, 'public2');
        }

and for displaying this image (in blade file):

<td><img src="{{asset('/storage2/image/blog/' . $post->cover)}}" width="50" height="50"></td>

Upvotes: 2

Loek
Loek

Reputation: 4135

How about this? It should even be OS-agnostic:

'uploads' => [
    'driver' => 'local',
    'root' => __DIR__ . '/../../uploads/'
]

Upvotes: 3

Related Questions