laravel storage method not displaying images on another OS or machine - laravel

I store images using this method:
'avatar' => $r->avatar->store('public/avatars')
and get them like
<img src="{{Storage::url($user->avatar)}}" style="border-radius:50%;height:30px;width:30px;" style alt="">
They get displayed on the local machine, but not in others. What could be the issue?

Check the value of user's avatar column, you might have some sort symlinks enabled on local machine, Instead you can also use public_path, as storage should not be exposed outside.
https://laravel.com/docs/5.6/helpers#method-public-path
Also check file visibility
https://laravel.com/docs/5.6/filesystem#storing-files

you have stored file inside public/avatars
be sure $user->avatar has public/avatars concatenated if not you might have to
{{Storage::url("public/avatars/".$user->avatar)}}
use this as source of your image

had to delete the public/storage folder on the other machine then run
php artisan storage:link
images started showing as normal

Related

Laravel storage link in shared hosting

im having problem with setting up storage link on shared hosting.
Because of that I can't upload any image to storage folder in shared hosting
Just create a route and access it once.
Route::get('generate', function (){
\Illuminate\Support\Facades\Artisan::call('storage:link');
echo 'ok';
});
Remove storage folder in public_html/public/storage if you perfomed storage:link in local
then Create a php file for example mysymlink.php in public_html folder
Add below code to mysymlink.php file
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink completed';
Go to web site example.com/mysymlink.php
Using Laravel in shared hosting environment probably not the best decision. Some many good options are now available for the same price and you just need to put a bit of effort to understand how to work with them, even if you have no prior experience. You can use DigitalOcean, Linode, Vultr or many more and it will save you a ton of time.
In case you for some reason still want to use shared hosting and to actually answer your question you can
Investigate to set a right symlink (This possibility nowadays exist almost everywhere) it should be something like ln -s /home/user/laravel/storage/app/public /home/user/public_html/storage. Of course it should by your path which can be different depending of your hosting structure, you should be able to figure out it yourself.
Try to implement in some workaround way. For example you can try to put files from storage to storage/app/public folder manually (you even can latter automate it by using some script). But workarounds product new workarounds, better not do it hard and wrong way but instead use simple and right even if it seems hard for you at first glance.
Instead of using Storage facade, you can use custom method to upload file to your public directory
if($request->has('image')){
$img = $request->file('image');
$ext = $img->getClientOriginalExtension();
$name = time().'.'.$ext;
$path = public_path('\img\uploads');
$img->move($path, $name);
User::where('id', $user->id)->update([
'image' => $name
]);
}

How to store images with heroku laravel app

With store route in laravel i pass data (text) to MySQL and image to storage. However i cant store images on local storage (after new deploy) changes in file system resets so i need to do it with external storage. Whats the easiest way to store images (low size) and to have permission to fetch them (with js)?
You need to store them to something persistent, like Amazon S3.
You can use Laravel's Storage system for this. Once you've set up an S3 disk with the right AWS credentials, it's as simple as:
$path = $request->file('your-field')->store('your-folder', 's3')
or:
$path = Storage::disk('s3')->put('path/to/folder', new File('/path/to/temporary/file'));
I encountered the same problem recently and here is what I figured out for a solution:
Heroku does not have storage for images, which calls for using a service that stores images for free and use it.
OR
You can just move them to the public folder instead of the storage folder, and use this in your view code:
<img src="{{ secure_asset($whereEverYouStoredYourImage->theImageAttribute) }}" alt="example">

Laravel 5.6 custom storage link/mount to other location on Windows OS

Laravel 5.6 custom storage link/mount to other location on Windows OS
https://laravel.com/docs/5.6/filesystem#configuration
php artisan storage:link will point the
http://localhost/storage to c:/project/storage/app/public
means visit http://localhost/storage/image.jpg = c:/project/storage/app/public/image.jpg
but how to custom the uri link to other location ? eg..
http://localhost/ramdisk/image.jpg link to z:/ramdisk/image.jpg
or
http://localhost/avatar/image.jpg link to z:/avatar/image.jpg
First of all: why would you like to do that? The images you upload should be in /storage/app/... or in /public/images/... Other disks may be unavailable if putting the app on a server.
To clarify, the php artisan storage:link only creates a symlink in /public/ folder pointing to the app/public. (I think in 5.6 it has absolute path so on server or after moving the application you have to delete and generate it again.)
So if you really want to access disk z on your PC from the app you should create a symlink in /public/ folder with the desired name. For example:
http://localhost/ramdisk/... would be a symlink named ramdisk in the public folder and would point to z:/ramdisk/. Then you would be able to access the folder contents as you wish.
Of course you can have multiple symlinks so you would create another one for the avatar folder too. Also make sure that disk z has a public access.
I was not able to test it, but I am sure that it would work, however I would not recommend it. If you want to use something in your app it should be within the app. (Except S3 drivers like amazon cloud storage.) I hope it helped.

How to get path to images generated with webpack encore

I have one problem with the images I want to include in my e-mail with phpmailer.
I have tried to use swift mailer with my symfony app but not works I have tried all configuration (all host all ports all encryption etc) during 3 days I have called them etc without any success. It works in local not in remote env...
Anyway, let's go back to my problem.
I use webpack with encore (new way of setting assets for symfony4).
All my images are renamed in the public folder. myimagename.65342324b5.jg I/O myimagename.jpg when I run encore to deploy assets.
The problem is when I want to AddEmbeddedImage
let's supposed I have logo.png in assetsfolder.
When I deploy assets it is renamed logo.536324324V3.png in public images folder.
So when I do
$mail->AddEmbeddedImage('images/logo.png','mon_logo', 'logo.png');
and when I want to refer in my html body of the e-mail to 'mon_logo' the image is not loaded because it is not existing only logo.536324324V3.png.
I have 2 options:
First I disable the automatic renaming of my images with webpack
encore .but I don't know how.
Second: I make this script able to recognise logo.png as
logo.536324324V3.png. Normally it is automatic with the
manifest.json generated after the assets deployment, but in my case
the path of my image is in my controller so I cannot specify that it
is an asset.
But I cannot do
$mail->AddEmbeddedImage(" {{ asset('images/logo.png')}}",'mon_logo',
'logo.png');
Anyway it makes no sense.
So if you have solutions I'll take it:)
Thank you :)
Assuming you are using the Asset-component as suggested in your second approach, you could just pass in the class \Symfony\Component\Asset\Packages which should be available under the service id assets.packages and with Symfony 4 likely also under it's fully qualifieed class name. The Packages class manages your assets and is also what's used in the Twig_Extension in the asset() function.
So in your code you would probably do something like this:
$fileUrl = $packages->getUrl('images/logo.png');

Issue with laravel file uploads on local storage

I am using
$request->file('photo')->store('public/photos');
to store files in public/photos directory under storage.
Created a link to storage from public directory using artisan storage:link. The output of
$request->file('photo')->store('public/photos');
is stored in db against the photo field. While retrieving the photo in view, I use asset() helper which output the link as
domain.com/storage/public/photos/photo.jpg
which returns 404 error. It should actually return
domain.com/storage/photos/photo.jpg
as public/storage is linked to app/storage/public. Please help.
You save it like this... $request->file('photo')->store('public/photos');
Store is the location to save the photo.
Remove public from here ->store('public/photos');

Resources