Laravel Storage : 403 Access denied - laravel

Im using laravel 8. I'm having access denied to Storage.
The public folder is location in www/projectfolder and laravel files are in www/projectfolder/framework
I created a symbolic link
App::make('files')->link(storage_path('app/public'), public_path('../../storage'));
The upload is working fine I can find the uploaded files in framework/storage/app/public using FTP
the {{asset('myfile.jpg')}} gives the link www.website.com/projectfodler/storage/myfile.jpg but this link is giving :
Forbidden
You don't have permission to access this resource.
I cannot run any command as i'm on shared hosting with very limited control panel
any suggestion

I fixed the issue doing the following :
config\filesystems.php
from
'links' => [
public_path('storage') => storage_path('app/public'),
],
to
'links' => [
'/home/USER/www/PROJECTFOLDER/storage' => storage_path('app/public'),
],
app\Providers\AppServiceProvider.php
public function register()
{
$this->app->bind('path.public', function() {
return realpath('/home/USER/www/PROJECTFOLDER/storage');
});
}
routes\web.php
Route::get('/symlink', function () {
Artisan::call('storage:link --relative');
});
Open www.website.com/projectfolder/symlink then remove the above from web.php
and all are fine now.
Thanks

Related

Laravel Symoblic link not working on shared hosting

I modified the using filesystem configuration as
'local' => [
'driver' => 'local',
'root' => public_path('images'),
'throw' => false,
],
So on local system, I'm storing the all the files in public directory
localhost:8000/public/images
by using file upload
$this->logo->storeAs('brand', 'abc.jpg');
I can directly access images using
url('images')
but when i upload my project to live, images are not displaying. I tried and executed the following route, but fail to load images on live server
Route::get('/link', function () {
$target = public_path();
$link = $_SERVER['DOCUMENT_ROOT'] . '/new';
if (symlink($target, $link))
die("SUCCESS");
else
die("ERROR");
});

how to upload images in public_html not in project file in laravel?

I upload my Laravel app to C-panel, my public contents are in public_html and I change index.php on public to this :
require __DIR__.'/../thermotajhiz/vendor/autoload.php';
$app = require_once __DIR__.'/../thermotajhiz/bootstrap/app.php';
And other files in main root named thermotajhiz
Everything is ok but when I want to upload an image, instead of upload to public_html, a file with public name will create in thermotajhiz file outside the public_html
how can I fix this ?
I'm using this plugin for upload files :
alexusmai/laravel-file-manager
in config/filesystems.php set like this
// Filesystem Disks
'disks' => [
'public' => [
'driver' => 'local',
'root' => '../public_html',
'url' => env('APP_URL'),
'visibility' => 'public',
],
],
if doesnt work try to change ../public_html to ../../public_html or /home/yourcpanelusername/public_html
and also make sure in your config/file-manager.php you set:
'leftDisk' => 'public',
I put this code to index.php instead of register method in AppServiceProvider and it's work :
$app->bind('path.public', function() {
return base_path('../public_html');
});
After this code :
$app = require_once __DIR__.'/../thermotajhiz/bootstrap/app.php';

Restrict direct access to image url in laravel

If i give image path directly it accessible now. I want image accessible only for if user loged in.
Ex:
This is image path http://localhost:8000/images/test.jpg
If user give this path without loged in through error message as access dined
Can you help me..
There are several ways to fix this problem, the way I would use is to save all images in your resources/assets/ folder outside of the public folder.
Now you could create a route like this:
Route::get('images/{file}', 'ImageController#getImage')->where('file', '.*');
And now in your ImageController, you can check if the user is logged in. If so, you can load the image from the resources folder and send it in a file response. If they are not logged in, there is no way they will be able to access the file.
Tested with Laravel 8
Define a new disk in file: config/filesystem.php
'disks' => [
// ...
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
'url' => env('APP_URL').'/storage',
'visibility' => 'private',
],
],
Define web route in file: routes/web.php
Route::any('/private{any}', function (Request $request, $file) {
abort_if(!Storage::disk('private')->exists($file), 404, "File doesn't exist.");
return Storage::disk('private')->response($file);
})->middleware('auth')->where('any', '.*');
Upload image file to folder: storage/private
Test on your localhost at: http://localhost:8000/private/image_name.jpg

How to change storage path in laravel 5.3 to public?

In laravel 5.3, if I upload a file from a form, it will be stored in own storage directory. Then I should create a symlink to that storage path in public path.
I can't use creating symlinks on my system because of the limitation. How can I upload files directly to public/uploads instead of storage/app/uploads?
I tried to set the path in AppServiceProvider.php but it doesn't work.
public function register()
{
//not working:
$this->app->useStoragePath( $this->app->basePath() . "/upload");
//also not working:
// $this->app->bind('path.storage', function () {
// return $this->app->basePath() . '/upload';
// });
}
by "not working" I mean that it is still uploading into the default storage directory. I cleared also the cache.
Here is the upload part (used in a controller):
$request->image->storeAs("uploads", "uploaded_image.jpg");
Thanks for any hints. :)
In Laravel 5 you now have to do this in config/filesystems.php. You can add a new disk like so:
'disks' => [
'uploads' => [
'driver' => 'local',
'root' => public_path(), // previously storage_path();
],
]
and then do the following to use it:
Storage::disk('uploads')->put('filename', $file_content);

Laravel socialite error 404

I followed this tutorial on how to use socialite -> https://laracasts.com/series/whats-new-in-laravel-5/episodes/9
This is what I've done:
Added to my composer.json
"laravel/socialite": "^2.0",
"laravelcollective/html": "5.1.*#dev"
Added to my app.php
providers
Laravel\Socialite\SocialiteServiceProvider::class,
Collective\Html\HtmlServiceProvider::class,
aliases
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
Added to services.php
'github' => [
'client_id' => 'myid',
'client_secret' => 'mysecret',
'redirect' => 'http://inspectorhost.app/login',
],
Added to routes.php
Route::get('/', function () {
if (Auth::check()) return 'Welcome back, ' . Auth::user()->username;
return 'Hi guest. ' . link_to('login', 'Login with Github!');
});
Route::get('login', 'AuthController#login');
Created an AuthController and added
class AuthController extends Controller
{
public function login(){
return \Socialite::with('github')->redirect();
}
}
I'm using nginx and I set it up so it doesn't need to use the public folder to acccess the web
root home/inspectorhost.app/public_html/public/;
So when I click on 'Login with Github', instead of sending me to Github to authorize the login, it sends me to /login and gives me a 404 error.
EDIT
Now it gets interesting... when I go to http://inspectorhost.app and click in 'Login with Github' I get the error from above (404) however if I run php artisan serve and I access the app from localhost:8000 it works... so I guess it might be some misconfiguration on nginx?
Any help is appreciated.
Open in private window. because after first time session store in github if your github login in other tab

Resources