Problem uploading images in cpanel in laravel 7 - laravel

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/');
});

Related

Collect multiple files into single PDF

I use in my project barryvdh laravel-dompdf package.
Users might generate a PDF with some personal information, till here everything is ok:
$pdf = PDF::loadView('personalInfo', $userData);
$pdfFileName = "personal-info-{$userData['username']}.pdf";
return $pdf->download($pdfFileName);
Admin should be able to download information of users, and in case of selecting several users, instead of generating several files I would like to collect all users PDFs into a single file.
How can I do it?
You can create like this below
For example you have personalInfo blade template which is now for particular user .So loop that view and store it in array like below
$pages=[];
foreach($userDataList as $key=>$userData){
$pages[]=(string)view('personalInfo', $userData);
}
$pdf = App::make('dompdf.wrapper');
$pdf->loadView('index', ['pages' => $pages]);
return $pdf->stream();
then you can create seperate blade template to show these views.Here i will name it as index.blade.php
<style type="text/css" media="print">
div.page
{
page-break-after: always;
page-break-inside: avoid;
}
</style>
#foreach($pages as $page)
<div class="page">
{!! html_entity_decode($page) !!}
</div>
#endforeach
Method:2
loop all user detail inside personalInfo blade template and at the end of each loop add below page break style
<p style="page-break-before: always;"></p>
Example inside personalinfo blade
#foreach($pages as $page)
personal information content
<p style="page-break-before: always;"></p>
#endforeach
Also i personally prefer laravel snappy for multiple page because it render faster than laravel dompdf.Only thing is to server configuration bit head ache if you are using windows
Ref:https://github.com/barryvdh/laravel-snappy

storage file path issue it repeat on serer side

My issue is simple but critical as well. The problem is asset function created different paths for local and staging. On local it creates perfect path but on server it creates incorrect one. On local it creates
http://localhost/storage/images/img.png
but on server it creates
http://domain/storage/images/storage/images/img.png
why its repeating storage/images 2 times on server.
<a class="start-stream open">
<span class="fas fa-plus-circle"></span>
<div class="text">Create Session</div>
</a>
#foreach($areaOfInterest as $interest) #php $str2 = parse_url($interest->image) #endphp
{{$interest->name}} #endforeach
</div>
Just do this
{{ asset('images/' . $str2['path']) }}
I assume you created symlink from storage to public on your server

Laravel pagination with URL parameter

I have a Laravel application. One of the pages can be reached via the following URL
http://localhost:8000/items/gallery?item_type=glasses
As the amount of items to be shown can be quite substantial, I'm using pagination. I have the following code in my view:
#foreach($media as $media_item)
<div class="col-md-3">
<div class="card">
<img class="card-img-top" src="{{ asset('storage/'.$media_item->id .'/'. $media_item->file_name) }}" ">
</div>
</div>
#endforeach
{{ $media->links() }}
and in the controller, I'm using:
$media = Media::paginate(5);
The pagination buttons are shown and work for the 1st one. Then when I click on the second (or third or fourth...) one, I get the following error message:
Method Illuminate\Database\Eloquent\Collection::links does not exist.
I see the link is trying to reach:
http://localhost:8000/beeritems/gallery?page=2
whereas I need:
http://localhost:8000/beeritems/gallery?item_type=glasses&page=2
In Laravel, how can I change the links() method to include the part after the question mark?
You must use ->appends() methods
$media = Media::paginate(5);
$media->appends($request->all());
you can use laravel basic URLs instead of getting gallery images with URL get parameters.
something like this:
define Route like this
/items/gallery/{types}
then using it like
http://localhost:8000/items/gallery/glasses
in this case you don't get that error anymore

NotFoundHttpException error in laravel

This is my view:
<img src="../public/{{ $axid->img }}">
and this is my route :
Route::get('/ax/{axid}', function($id) {
$axid = show::find($id);
return view('ax', compact('axid'));
Images are uploaded in public/uploads and show is model name. However, I have this error:
NotFoundHttpException
...what is the problem?
If we assume that your image resolving code is functioning properly and that you are returning the full url of the image you want to show, then change your view code to:
<img src="{{ asset($axid->img) }}">
Please see how you should be setting this out:
View before moving to this a href..: Get ID
Route: Route::get('/ax/{id}', 'AxidController#get');
Controller with me assuming Show is your model name:
public function get($id, Show $show)
{
$axid = $show->find($id);
return view('page', compact('axid'));
}
On the page of ax/{id}:
<img src="{{ $axid->image }}">

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