Accessing public directory laravel 5.5 deployed on shared hosting - php

I deployed a Laravel 5.5 application on shared hosting and I have a problem with uploading/viewing images.
Everything is working well on localhost but when I upload it online I can't access the directory where my images are stored.
Here is where is store my images
$destinationPath = public_path('items/'.$request->get('modal-item-id'));
And it is storing image in :
public_html/nbd/public/items/307/my-image.png
Also here is my filesystems.php settings for 'images':
'images' => [
'driver' => 'local',
'root' => public_path('items'),
'url' => env('APP_URL').'/items',
visibility' => 'public',
],
My app is located in
public_html/nbd/system
...and because I had to put PUBLIC folder out of the systems folder (shared hosting) now I can't access the image on https://my-url/system/public/items/307/my-image.png
How can I access that file? Where did I make a mistake?

You should not run the full Laravel under a webserver document root which is in your case ./public_html more point the documentroot of the webserver to ./public_html/nbd/system/public -> and you will be able to access http://your-domain/items/307/my-image.png
But in your case you cannot access due missing folder nbd -> https://my-url/nbd/system/public/items/307/my-image.png

Related

Laravel file image not show in the production server

When I run in my local, the image file is shown. But in the production server it is not showing.
URL in my local :
http://localhost:8000/files/image/GOT7.jpg
URL in my production server :
http://myApplication.com/files/image/GOT7.jpg
.env in the local :
APP_URL=//localhost
.env in the production server :
APP_URL=https://myApplication.com
config/filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('/app'),
],
],
file location in the folder public/files/image/GOT7.jpg
How to solve it?
I'm gonna take a guess here and say that it is a symbolic link problem.
https://laravel.com/docs/8.x/filesystem#the-public-disk
You are using a filesystem named local which has its root in storage_path('/app'). This is a reference to the storage directory in your Laravel project.
This directory by default is not accessible. Nor should it ever be. Only your public folder should be accessible from the internet (so everything goes through public/index.php only).
So a solution that Laravel offers is to place a symlink in your public folder to a particular storage path. See the link above.
php artisan storage:link
This would create a symbolic link for you in public/storage to storage/app/public. This should now make your files accessible. For more finetuning see the Laravel docs link above.

Laravel Nova uploaded image file not showing on staging environment

I have deployed a laravel application which is using the Nova admin for backend management on Bluehost VPS account. I have creates a symlink between storage and public as recommend in the laravel documentation.
I have set up a resource for uploading images like so
Avatar::make('Image')->onlyOnIndex(),
Image::make('Image')->disk('public')
->path('item-category')->required()->hideFromIndex()
I am using the public disk for storage with the filesystem disk configuration like so
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
On my local system, I am able to see the actual image displayed but on the staging the image is uploaded successfully but does not display because the image is not found
The record is stored in the db like so
Why does this same configuration work on my local dev system but not on the staging server? Spent the best part of my morning trying to figure this out.
Try running the artisan command php artisan storage:link, that will create a symlink from your public folder to the storage folder.

Using storage_path from another Laravel app

I have an application that uses two different Laravel apps talking to the same database. App 1 is called BUILDER and App 2 is called VIEWER. In production I use S3 for storing files submitted within the application. For local development I use the storage/app/public folder in BUILDER.
The local dev setup is that BUILDER runs on localhost:8000 and VIEWER on locahost:8001
Now here comes my problem. In production both apps use the same S3 bucket for storage. So somehow I need to set this up similarly for local development.
The BUILDER is working fine, uploading and reading its files from the storage/app/public folder with FILESYSTEM_DRIVER=public in .env
The VIEWER is also reading these files fine, creating correct URL's after I added a new disk in the config (BUILDER_URL is set in .env to localhost:8000 which is the URL for the BUILDER)
'builder_public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('BUILDER_URL') . '/storage',
'visibility' => 'public',
],
BUT... I need to somehow be able to also upload files from the VIEWER app that should end up in the same storage folder as the BUILDER.
So in my VIEWER app I would like my builder_public disk to be something like this:
'builder_public' => [
'driver' => 'local',
'root' => builder_storage_path('app/public'), // here
'url' => env('BUILDER_URL') . '/storage',
'visibility' => 'public',
],
Is there some way I can share the storage/app folder between two separate Laravel apps?
Yes, you can. if you are using Linux server, try below command
ln -s SOURCE_FOLDER DESTINATION_FOLDER
it will create folder short cut but still, your both application will use the same location(DESTINATION_PATH)
If you are using different servers, try mount command.

Laravel error: symlink() has been disabled for security reasons

I'm trying to set-up a laravel cms system that already exists on a shared host. But when i setup my laravel project on this shared hosting directory i get the error
symlink() has been disabled for security reasons
I can't seem to find a proper explenation on how i can fix this in my situation. I only get this error the first time and when i refresh this error, it disappears.
in your project's public directory, manually create a storage folder.
this worked for me
You can create symlink for your desired directory by logging in to your server via SSH download Putty and then login to your server via putty by using your server credentials. You can use linux commands to create a symbolic link:
ln -s TARGET LINK_NAME
Where the -s makes it symbolic.
There are 2 options to solve this;
Use any of the supported services for file storage i.e Amazon S3, Dropbox, FTP, Rackspace
Follow the steps below;
Create a folder in /public folder named /storage
Move all folders from /storage/app/public/ to the folder you created /public/storage/
Open file config/filesystems.php and change the values as shown;
'local' => [
'driver' => 'local',
'root' => public_path('storage'),
],
'public' => [
'driver' => 'local',
'root' => public_path('storage'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
],
Now open file .env and change or create the values as shown below;
AVATAR_DIR=avatars
SIGNATURE_DIR=signatures
LOGOS_DIR=logos
MEDIA_DIR=media
After a lot struggle this trick work for me at shared hosting server for Laravel project.
Connect with terminal at server via SSH or directly from CPanel
Run this command at your terminal
ln -s /folder-path-with-name-to-be-linked /folder-paht-where-you-want-to-link-it
e.g in my case
ln -s /home/user-name/public_html/domain-folder-name/storage/app/public /home/user-name/public_html/domain-folder/public/
Note: You don't need to change in php.ini and .htaceess files.
Reference Link:
https://unix.stackexchange.com/questions/141436/too-many-levels-of-symbolic-links/141442#141442?newreg=14b52f3d6fcb454399a1a1a66e2170df
The problem could be that your hosting provider will not let you create the smlink.Have you checked if this is the case?
One suggestion if so, is to write the files that you would have put in the symlink folder directly into the public area of the site so that a symlink is not required.

Managing uploaded files in Laravel - linking public and storage directories

In my Laravel 5.1 app, I'm storing images in storage/app/uploads folder.
My local disk:
<?php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app/uploads'),
],
// other configuration...
?>
Thing is, I can't figure out a way to use the image files after they're uploaded, e.g. as the source of <img> tag. Basically, I need to retrieve a valid path to an image, so it can be used on the page.
For deployment I'm using Envoyer which provides a solution. According to Envoyer docs:
When storing user uploaded files, you should store them in the storage directory of your application if you are using Laravel. Then, you may use the "Manage Linked Folders" feature of Envoyer to create a symbolic link from your public directory to the storage directory. The "Manage Linked Folders" button can be found on the "Deployment Hooks" tab of your project.
..and this is clear.
But how do I "link" the storage and public folders in my local development environment? Does Laravel provide a way to do it, or do I need to create a symbolic link in my environment manually?
There are a few options:
Create a controller that would output the files
class AssetController {
public function show($id) {
$file = File::findOrFail($id);
return Response::make(Storage::get($file->storage_key), 200, ['Content-Type' => $file->mime_type]);
}
}
Create a symlink public/assets => storage/app/
Upload files to public/assets instead of storage/app
Use rewrites on your web server to serve the files from your storage/app folder - how to do that depends on what webserver you are using. For nginx you could use something like
rewrite ^/v1/assets/(\d+) /../storage/app/$1;

Categories