Laravel upload file outside root path - laravel

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'
);

How about this? It should even be OS-agnostic:
'uploads' => [
'driver' => 'local',
'root' => __DIR__ . '/../../uploads/'
]

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>

Related

Laravel Voyager: Repeating "App/Public" in image url on admin side

When I add a restaurant/upload an image using voyager. The image displays correctly on user side but doesn't show on admin side. when i inspect the url of the image it shows "app/public" is repeating twice.
If I update the restaurant info and let the image be the same. then it even stops working on the user side too. After inspecting the URL i got to know that the "app/public" keeps repeating the number of time i update the restaurant.
Images directory
public_html/tmo_api/storage/restaurants/July2021
**I've already tried symlinking again. and cache clear etc...
**
Voyager.php
/*'storage' => [
'disk' => env('FILESYSTEM_DRIVER', 'public'),
],*/
'storage' => [
'disk' => 'voyager',
],
filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage/app/public',
'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'),
],
'voyager' => [
'driver' => 'local',
'root' => storage_path('app/public'),// change here something specific to your application need
'url' => env('APP_URL').'/storage/app/public',
'visibility' => 'public',
],
],
app.php
'url' => env('APP_URL', 'http://example.com/tmo_api'),
'asset_url' => env('ASSET_URL', null),
.env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:sdfswm8pJN6QQls+sdfRfsd56NQw8XBsdsdfdSW/saVPQ=
APP_DEBUG=true
APP_URL=http://example.com/tmo_api
You can see it repeating on ID-6

File not found at path in laravel 7x

I have an image at storage/public and I want access that with Storage library but i get error "File not found at path"
I search a lot in the internet and I don't get any solution for my problem so please help me.
my code :
$filename = '123.jpg';
$path = storage_path()."/".$filename;
Storage::disk('local')->get($path);
dd($contents);
the error :
Illuminate\Contracts\Filesystem\FileNotFoundException
C:\xampp\htdocs\admin\storage/123.jpg
the things i tried :
php artisan storage:link
the filesystem.php
<?php
return [
'default' => env('FILESYSTEM_DRIVER', 'local'),
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
]
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL') . '/storage',
'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'),
],
],
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
Your file is under storage/public folder but you trying to access it from storage folder.
Check your error path C:\xampp\htdocs\admin\storage/123.jpg
Change
$path = storage_path()."/".$filename;
to
$path = storage_path('public/' . $filename);

Lumen Storage - FileNotFoundException

I added flysystem to Lumen so I can use the Storage Facade.
I added the correct config file and then I'm trying:
$template = Storage::get(storage_path('a.html'));
Config:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
'url' => env('APP_URL').'/storage',
],
'public' => [
'driver' => 'local',
'root' => storage_path(),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
I'm getting:
FileNotFoundException /var/www/storage/a.html
The file exists in that location.
Any clues?
Try Storage::get(“a.html”) instead

Override Laravel Voyager default Image Upload Path

The Voyager by default uploads the images to storage/app/public/posts folder for posts' featured images.
I want to change it to a custom folder in my public folder.
How do I achieve this?
Thanks in advance.
Open config/filesystems.php file and check this section. Add one more key like voyager for example.
'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_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
],
'voyager' => [
'driver' => 'local',
'root' => storage_path('app/public'),// change here something specific to your application need
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
],
Then open config/voyager.php file and set that key ('voyager') instead of 'public' like this:
'storage' => [
'disk' => 'voyager',
],
Go to the config/filesystems.php and change the root and URL key for your new folder. See below the example
'public' => [
'driver' => 'local',
'root' => public_path().'/app', //Add new folder into public folder called app
'url' => env('APP_URL').'/public/app', //change get path for image
'visibility' => 'public',
],

storing files to aws s3 using laravel

Please give me an example to store files to AWS S3 in laravel 5.5.
I already configured filesystem file with AWS credentials.
I saw the function
Storage::put('file.jpg', $contents);
but don't know what the second parameter is
the contents of filesystem.php file is as follows
return [
'default' => env('FILESYSTEM_DRIVER', 'local'),
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
'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' => '*********',
'secret' => '******',
'region' => 'my region',
'bucket' => 'bucketname',
],
],
];
thanks in advance
There is good example in Laravel documentation:
Storage::disk('s3')->put('avatars/1', $fileContents);
The 2nd parameter is is file content so in real life it could look like this:
Storage::disk('s3')->put('file.jpg', file_get_contents('your_local_file.jpg');

Resources