Images in app/public (laravel) not show in Heroku app - php

I have a project in Laravel in which one of the sections uploads images to the server. Images are saved by using File storage and in the / storage/app/public folder.
In local it works correctly and the images look good, but when I upload the project to Heroku, the images are not seen. Even in Heroku if I run this command there are issues:
php artisan storage: link
Why is it that the images are not visible? I would not want to use AWS S3 for this. What could I have missed?

In your composer.json add:
"post-install-cmd": [
"ln -sr storage/app/public public/storage"
],

Heroku doesn't have file storage. So if you're going to use it for your servers you need to figure out another way to store files.

What I did that worked for me:
In your composer.json add:
"post-install-cmd": [ "ln -sr storage/app/public public/storage" ],( don't forget the comma , if you have any other line after the command above) But you should add the line above in scripts section.

Also, you need to run the command:
php artisan storage:link
Before running this command please check if the storage folder is already there or not inside the public folder if it's there then rename it to storage-bk and run the command. This way it creates new shortcut folder storage (storage/app/public).

This works for me:
You can add to your command line in your Procfile.
worker: php artisan storage:link
After adding this please push your project to Heroku again.

Also, you can upload images to the public folder instead using of a storage folder, by this method you will not get problems like the above.
for that, you need to change the config in app/filesystem.php
Change from:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Change into:
'public' => [
'driver' => 'local',
'root' => public_path(),
'url' => env('APP_URL').'/public',
'visibility' => 'public',
],
And you can store files so:
$pathToFile = Storage::disk('public')->put('uploads/', $file);
Now you can insert $pathToFile variable to the Database!

Related

Laravel 7, how to avoid image upload to app/public/ but instead public_html directory

I am trying to upload the image to ./../public_html directory in laravel 7. However, the image always goes to lsapp/app/public/ directory.
In my Controller,
$storagePath = $request->donation_poster->storeAs('public/donation-poster', 'donation-poster-'.time().'.jpg');
config/filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'links' => [
public_path('donation-poster') => storage_path('app\public\donation-poster'),
]
I think maybe the php artisan storage:link command isn't call, but i have no idea how to call using cpanel as it doesn't provide a terminal.
I know what is issue, my directory in public_html isn't linked with the one in public/storage. The command solve my issue,
ln -s <target> <link>

Laravel 9 storage returns 404 on live server

I am trying to show images from my symlink storage directory on my live server (shared) the scripts are working perfectly on my localhost but by the time i deployed it the image that was uploaded is not showing and returns page 404 error.
So i run the following command before deploying.
php artisan config:cache
php artisan route:cache
php artisan view:cache
and then run a symbolic link for the storage using a php script
symlink('/home/cpanelUsername/project_folder/storage/app/public', '/home/cpanelUsername/public_html/www.domain.com/storage');
My filesystem is set to public as below.
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
I have an image uploading and resizing function on my application and everything is storing just fine.
$request->file('profile_image')->storeAs('public/profile', $filenametostore);
$request->file('profile_image')->storeAs('public/profile/thumbnail', $smallThumbnail);
$ThumbNail = 'storage/profile/thumbnail/'.$smallThumbnail;
$this->createThumbnail($ThumbNail, 150, 93);
But the image is not loading or showing using below code. any idea why this is happening on live server and completely working fine on my local i also ensured that folder privilege have read and write.
asset('storage/profile/'.$member->profile_image)
I manage to solve it by changing FILESYSTEM_DRIVER=local to FILESYSTEM_DRIVER=public on my .env file

How to display image from another disk in Laravel

I am trying to display an image in my Laravel application. The image comes from another disk which is a samba share. I followed the configuration instructions from the Laravel Doc, here is my setup:
filesystems.php
'shop_storage' => [
'driver' => 'local',
'root' => env('SHOP_STORAGE_PATH', base_path('shop_storage')),
'dir' => [
'public' => 0777,
'private' => 0777,
]
],
My samba share is mounted into the root folder /shop_storage inside my Laravel application with the following command:
sudo mount.cifs //PATH_TO_FILE_SHARE/ARMMAG ./shop_storage -o credentials=$HOME/.smbcredentials,rw,file_mode=0777,dir_mode=0777,mfsymlinks
I correctly see all files when I open the folder.
Then, I added a Symlink from /public/storage to /shop_storage with the following command:
sudo ln -s /shop_storage /public/storage (Using full path starting at /home, removed here for privacy)
When I open /public/storage, I correctly see the symlink pointing to the samba share. If I click to open it, I see all the files. The file I am looking for exists.
The problem is when I am trying to display it in my webpage, the request return a 404.
Here is some of the things that I have tried:
<img src="{{\Storage::disk('shop_storage')->url('products/16/MAZJKj59EY0XQ57oKIibKr33eZ5dsqAPx44MCHHJ.png')}}">
<img src="http://localhost:8000/shop_storage/products/16/MAZJKj59EY0XQ57oKIibKr33eZ5dsqAPx44MCHHJ.png">
<img src="{{asset('products/16/MAZJKj59EY0XQ57oKIibKr33eZ5dsqAPx44MCHHJ.png')}}">
<img src="{{asset('storage/products/16/MAZJKj59EY0XQ57oKIibKr33eZ5dsqAPx44MCHHJ.png')}}">
What am I doing wrong?
Edit
After some try/error, I managed to get a different response with the following syntax:
<img src="{{asset('storage/shop_storage/products/16/MAZJKj59EY0XQ57oKIibKr33eZ5dsqAPx44MCHHJ.png')}}">
Now, I am getting the foloowing error
You don't have permission to access /storage/shop_storage/products/16/MAZJKj59EY0XQ57oKIibKr33eZ5dsqAPx44MCHHJ.png on this server.
My permission are set to 777, what could cause this?
What I ended up doing to make this work is mount my share directly into public/storage instead of using symlinks. Then any I could access the file using the laravel asset helper:
<img src="{{asset('storage/products/16/MAZJKj59EY0XQ57oKIibKr33eZ5dsqAPx44MCHHJ.png')}}">

Voyager for Laravel 5: Wrong paths for images

Having created a new user via Voyager, the avatar was showing as a broken image. I uploaded a new image, but the image remained broken.
I checked with Console in Google Chrome and found 3 404 errors:
2017-06-19 10:53:50.159
http://localhost:8888/storage/users/June2017/ypKlbqbATDbpnYctZBFp.png
Failed to load resource: the server responded with a status of 404
(Not Found)
2017-06-19 10:53:50.256
http://localhost:8888/storage/users/June2017/ypKlbqbATDbpnYctZBFp.png
Failed to load resource: the server responded with a status of 404
(Not Found)
2017-06-19 10:53:50.473
http://localhost:8888/vendor/tcg/voyager/assets/images/bg.jpg Failed
to load resource: the server responded with a status of 404 (Not
Found) When I looked at the project, the paths for the 2 storage
errors are: http://localhost:8888/storage/app/public/users/ and:
http://localhost:8888/storage/app/public/users/June2017
While in Media, I'm seeing similar path problems when navigating the folders:
2017-06-19 11:38:22.248 %7B%7B%20selected_file.path%20%7D%7D:1 GET
http://localhost:8888/development/PhpstormProjects/Search4Venues/public/admin/%7B%7B%20selected_file.path%20%7D%7D
404 (Not Found)
Anticipating the question, in .env, I have: APP_URL=http://localhost:8888
Is this a configuration error?
EDIT
A colleague with more experience than me has been through Voyager and found some inconsistencies, which he's attempted to fix.
In app.php, we've had to change:
'url' => env('APP_URL', 'http://localhost:8888/development/PhpstormProjects/Search4Venues'),
... to:
'url' => env('APP_URL', 'http://localhost:8888'),
In filesystems.php, we've had to change:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
]
... to:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/development/PhpstormProjects/Search4Venues/public/storage',
'visibility' => 'public',
]
However, he discovered other problems in config/voyager.php:
'assets_path' => '/vendor/tcg/voyager/assets',
... where it too appears to be missing PhpstormProjects/Search4Venues from the path:
http://localhost:8888/vendor/tcg/voyager/assets/images/bg.jpg
At this stage, we have no idea what else could be wrong with Voyager.
Firs u need edit file .env:
APP_URL=http://localhost
to:
APP_URL=http://yout-site-url
After that u need edit the file fylesystem.php like this:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/public/storage',
'visibility' => 'public',
],
I was helped
Follow these steps:
1.Change APP_URL in the .env file to your website url.
For example:
APP_URL=http://localhost
to
APP_URL=http://127.0.0.1:8000
2.Create symbolic link by: (if not done yet)
php artisan storge:link
3.Restart your server (must. Otherwise chages will not be applied.)
this is common problem that even official site of voyager is mention about just read this page https://voyager-docs.devdojo.com/troubleshooting/using-https
in summery just open file filesystem.php in the folder config and delete this line of code 'url' => env('APP_URL').'/storage',
Update you .env file
APP_URL=http://localhost:8000
and then run
php artisan config:clear
last
restart your serve
this work for me.
This error sometimes usualy shown if we runing php artisan serve with default port like localhost:8000, but if you place laravel wit voyager in htdocs/www in your own server like xampp/mamp etc this error will gone,
So you can try :
Installing apache or stand alone webserver like wamp or mamp
place your project in htdoc/www folder, and don't forget to set .env app_url:localhost (or your domain without port)
change document root in apache config to your project folder
acces http://[your_domain]/[your_project]
done..
thank's
Updated .env file:
APP_URL=http://localhost:8000
Fixed issue
dont forget to restart the server
Updated .env file:
APP_URL=http://localhost/voyager
And Updated filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage/app/public',
'visibility' => 'public',
],
All you need to do is change in .env
APP_URL = http://localhost
to
APP_URL = http://<whatever your site is called>
And then move to the pic files to
public users
For some reason voyager stores them in
public users\users
At least it did for me
Just check what URL Voyager creates for the avatars and move them into the public folder with the same URL.
Worked for me at least
Change one line in .env file:
APP_URL = http://localhost
to
APP_URL = http://<your site url>
The command is used to remove the configuration cache file and created again.
php artisan config:cache
if you using laragon, change
'url' => env('APP_URL').'/storage'
to
'url' => env('APP_URL').'./storage'

File not found at path in laravel 5.2

I have an image within the public/badges directory called 1.png - I am trying to use the Storage library in laravel but keep getting the following error even though the file does exist at that location:
// Error
FileNotFoundException in Filesystem.php line 381:
File not found at path: Users/gk/Sites/mysite/public/badges/1.png
// Code
$filePath = 'badges/1.png';
$path = public_path()."/".$filePath
Storage::disk('local')->get($path);
dd($contents);
Form Laravel 5.2 documentation:
When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory.
So You are looking for file in: storage/app/Users/gk/Sites/mysite/public/badges/1.png
Error is quite confusing.
[Update]
Add to config/filesystems.php
'disks' => [
//...
'local_public' => [
'driver' => 'local',
'root' => public_path(),
],
//...
],
then
$filePath = 'badges/1.png';
$content = Storage::disk('local_public')->get($filePath);
dd($content);
I had the same problem in Laravel 5.7, this fix my problem:
php artisan storage:link

Categories