Images not showing up on a mac - laravel

I have setup up a simple file upload system using laravel.
To store the image, I set up my controller as
public function store(Request $request)
{
$menu = new Menu;
$menu->title = $request->get('menuTitle');
$menu->user_id = Auth::id();
if($request->hasFile('menuImage')){
$filenameWithExt = $request->file('menuImage')->getClientOriginalName();
$fileName = pathinfo($filenameWithExt,PATHINFO_FILENAME);
$ext = $request->file('menuImage')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$ext;
$path = $request->file('menuImage')->storeAs('public/menu',$fileNameToStore);
}
else{
$fileNameToStore = 'blank.jpg';
}
$menu->image_name = $fileNameToStore;
$menu->save();
return redirect(route('menu.create'))->with('success',$menu->title." Added successfully");
}
In the view I set it up as
<a href="#">
<img class="img-fluid rounded mb-3 mb-md-0" src={{asset("/storage/menu/".$menu->image_name)}} alt="">
</a>
In the browser the link is
<img class="img-fluid rounded mb-3 mb-md-0" src="http://127.0.0.1:8000/storage/menu/Screenshot_20190517_023111_1558916284.png" alt="">
While developing, I was working on ubuntu and everything works fine. However, I transferred my files to my mac and using xampp I was able to get everything working. However the images do not load anymore. I get a 404 error when I check the console. However, the images are able to upload to the right folder.

You should run php artisan storage:link so that the storage/app/public will be linked to public/storage directory and your link will work again

Related

download existing zip folder from laravel storage

I have a zip folder under storage and I want to download this folder whenever I click a button. But my code does not work. What is the problem?
here's my livewire component
<div class="my-auto">
<a type=button wire:click="downloadKit" style="border-radius:4px" class="">{{ __('Download') }}</a>
</div>
public function downloadKit(){
$file = Storage::disk('public')->get('kit.zip');
return (new Response($file, 200));
}
You can return a file response like this
return Storage::download("/path/to/kit.zip", "kit.zip");
The path will be relative to /storage/app

File does not exist maatwebsite/excel laravel export from view

Tried to export an excel file through a Laravel blade but the issue exists when trying to add an image that is from external link not the same server (it's from google storage) the image is accessible through browser
The error is:
PhpOffice\PhpSpreadsheet\Writer\Exception File URL does not exist
Also, tried to get base64 and also didn't work
$type = pathinfo($url, PATHINFO_EXTENSION);
$data = file_get_contents($url);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
didn't work <img src='{{$url}}' style="height: 20px;" height="100px">
didn't work <img src='{{$base64}}' style="height: 20px;" height="100px">
I solved it by temporally download the image into the server and use the path to the image on the server.

Problem uploading images in cpanel in laravel 7

I am trying to upload images on a shared hosting but am unable to upload. On my machine it works well but when I host i cant figure how to change the paths.All the other pages are working fine except uploading images.
on my controller
foreach ($images as $image){
$move=$image->move(public_path().'/images2/',time().'_'.$image->getClientOriginalName());
if($move){
$imagedata=Images::create([
'title'=>time().'_'.$image->getClientOriginalName(),
'filename'=>time().'_'.$image->getClientOriginalName()
]);
Displaying the previous images is okay with this code on the view side
#foreach($product_images as $image)
<div class="carousel-item {{ $loop->first ? 'active' : '' }}">
<div> <img class="d-block w-100 newim" src="/images2/{{$image->filename}}"/></div>
</div>
#endforeach
i think your problem is here with the wrong "," :
$move=$image->move(public_path().'/images2/',time().'_'.$image->getClientOriginalName());
change it to:
$move=$image->move(public_path().'/images2/'.time().'_'.$image->getClientOriginalName());
and make sure your folders are same name as you wrote in your code because host is sensitive to uppercase or lowercase
I added this to index php though not sure whether it is secure but it works
$app->bind('path.public', function() {
return realpath(__DIR__.'/../public_html/');
});

how to render to blade the image stored in storage/app/public directory of Laravel 7.14?

I am stuck. how to display the image stored in /storage/app/public directory of laravel 7.14 ?
I already tried the ones I found in stack overflow, or even the ones i found by googling.
I tried this solution
//Controller function
public function renderPic($filename) {
$path = '/storage/app/public/uploads/'.$filename;
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file,200);
$response->header("Content-Type",$type);
return $response;
}
web route
Route::get('user/renderPic/{filename}', 'User#renderPic')->name('user.renderPic');
blade view file
<img id="displayPic" src="{{ route('user.renderPic',$user->filename) }}">
it doesn't show up
You don't need to define a route and controller method to render an image.
you simply open a terminal in the root directory for your laravel app and run
php artisan storage:link
this will make a symlink for the storage/app/public directory to public/storage
the in your blade file you simply render the image like so:
<img id="displayPic" src="{{ asset('storage/your-image-name.ext') }}">

Why is the avatar on app.blade is broken whenever I open links that has id?

Whenever I open an item the link would be
http://localhost/mywebsite/public/item/{$item_id}
the avatar will break. Below is the code I used
<img src="uploads/avatars/{{ Auth::user()->avatar }}">
The location of the image is C:\xampp\htdocs\mywebsite\public\uploads\avatars
I presume that as you are using relative URLs the translated path in browser will be http://localhost/mywebsite/public/item/{$item_id}/uploads/avatars/{{ Auth::user()->avatar }}
And that's not what you want
Try
<img src="http://localhost/mywebsite/public/uploads/avatars/{{ Auth::user()->avatar }}">
In production in will be just
<img src="/uploads/avatars/{{ Auth::user()->avatar }}">
Am not sure it will work on xampp though
So to say, I recommend using Laravel Valet or Homestead instead of Xampp, it cuts some problems like yours off.
in File model i created a function to get file encoded base64
public function getFile()
{
$path = storage_path('app'). "/" . $this->path;
$type = File::mimeType($path);
$file = Storage::disk('local')->get("/". $this->path);
return "data:image/png;base64,".base64_encode($file);
}
in blade You can use like that
<img src = '$file->getFile()'>

Resources