How to open file stored in stoarge folder in laravel? - php

I am trying to open files which is stored in "storage/app/public/cca_license/pdf_upload" folder. When I try to open the file, the content is empty.
What I have tried is
account.blade.php
<table id="table" class="table table-bordered text-center">
<thead>
<tr>
<th>PDF Name </th>
<th>Date</th>
<th>View</th><!-- comment -->
<th>Delete</th>
</tr>
</thead>
<tbody>
#if(count($pdf_data)> 0)
#foreach($pdf_data as $data)
<tr>
<td>{{$data->pdf_name}}</td>
<td>{{$data->pdf_date}}</td>
<td>
<i class="fa fa-eye"></i>
<!-- <iframe id="ifrm" src="/cca_license/pdf_upload/txtfile-converted_1640170538.pdf" >
</iframe>-->
</td>
<td><i class="fa fa-remove"></i> </td>
</tr>
#endforeach
#else
<tr><td colspan="4">No Pdf Listings</td></tr>
#endif
</tbody>
</table>
web.php
Route::get('/openPdf/{name}', 'HomeController#openPdf');
HomeController.php
public function openPdf($name) {
$contents=asset('cca_license/pdf_upload/'. $name);
}
When I try to open a file, i got blank white page without file contents.
How to view contents along with file in browser.

Your code does not contain any return statement. Laravel offers a variety of ways to make responses.
https://laravel.com/docs/8.x/responses
For example, a file response.
public function openPdf($name)
{
return response()->file(storage_path('app/public/cca_license/pdf_upload/'. $name));
}
The asset() helper returns a URL. If the file is stored locally, you should refer to a local path.

Related

How can I display Laravel images on a live server?

I developed a laravel app which is working perfectly on my local machine together with the images. On deployment to live server, the images are no longer visible. I am using shared hosting and my folder structure is like this: public_html containing the public files like upload and main containing the remaining files which should not be visible to the user. Here is my controller `public function
PortfolioAdd(){
return view('frontendbackend.portfoliosection.addportfolio');
}
public function PortfolioStore(Request $request){
$validatedData = $request->validate([
'title' => 'required:portfolio_sections,title',
'description' => 'required:portfolio_sections,description',
]);
$data = new PorfolioSection();
$data->title = $request->title;
$data->description = $request->description;
if ($request->file('image')) {
$file = $request->file('image');
$filename = date('YmdHi').$file->getClientOriginalName();
$file->move(public_path('upload/portfolio_images'),$filename);
$data['image'] = $filename;
}
$data->save();
`and here is my view
<table id="example1" class="table table-bordered table-striped" style = "color:white">
<thead>
<tr>
<th width="5%" style = "color:white">SL</th>
<th style = "color:white">Title</th>
<th style = "color:white">Description</th>
<th style = "color:white">Image</th>
<th style = "color:white">Action</th>
</tr>
</thead>
<tbody>
#foreach($allData as $key => $portfolio )
<tr>
<td style = "color:white"> {{ $key+1 }} </td>
<td> {{ $portfolio->title }} </td>
<td> {{ $portfolio->description }} </td>
<td>
<img src="{{ (!empty($portfolio->image))? url('upload/portfolio_images/'.$portfolio->image):url('upload/no_image.jpg') }}" style="width: 60px; width: 60px;">
</td>
<td>
Edit
Delete
</td>
</tr>
#endforeach
</tbody>
<tfoot>
</tfoot>
</table>
The easiest way to do it is to create a folder inside your public folder e.g portfolio_images.
if ($request->has('photo')) {
$photoName= time() . 'photo' . $request->photo->extension();
$request->photo->move(public_path('portfolio_images'), $photoName);
}
This will save it in public/portfolio_images.
Then in your view
<img id="previewImg" src="{{ asset("portfolio_images/$portfolio->image") }}" alt="Preview your image" height="150">
This should solve it!
First check whether ($portfolio->image) is showing any value by passing it directly in the tag i.e
{{ $portfolio->image}}
if it returns values, check whether the value exists in your folder.
If it exists ch then check your image src.
I would try the following(have not tested)
change this: $file->move(public_path('upload/portfolio_images'),$filename);
to: $file->move(public_path() . 'upload/portfolio_images',$filename);
You must create a symbolink link for that.
The default folder for uploading files is the storage folder.
By running php artisan storage:link, the application will generate a symbolic link that files will be accebile from the public area.
Google symbolic link. You will find the answer.

Delete the row after removing file from storage

everything works perfectly but it throw an error “Trying to get
property ‘path’ of non-object” on this line
“unlink(“uploads/”.$img->path);” and when I refresh the row and file
are deleted .if there is another way please tell me
blade :
<table class="table table-hover">
<thead>
<tr>
<th scope="col">image</th>
<th scope="col">title</th>
<th scope="col">Actions </th>
</tr>
</thead>
<tbody>
#foreach($data as $img)
<tr>
<th scope="row"> <img src="uploads/{{ $img->path }}" width="50%" /></th>
<td>{{$img->title}}</td>
<td>
<a href='#'><i class="fa fa-edit" id="updateIcon" wire:click="selectItem({{ $img->id }}, 'update')" ></i></a>
<i class="fa fa-trash" style='color:red;' wire:click="selectItem({{ $img->id }}, 'delete')" data-target="#modalFormDelete"></i>
</td>
#endforeach
</tr>
class:
public function selectItem($itemId, $action)
{
$this->selectedItem = $itemId;
if ($action == 'delete') {
$this->dispatchBrowserEvent('openDeleteModal');
}
}
public function delete()
{
$img=Caroussel_Img::find($this->selectedItem);
unlink("uploads/".$img->path);//bug here
Caroussel_Img::where("id",$img->id)->delete();
$this->dispatchBrowserEvent('closeDeleteModal');
}
It is not a good idea,
and I don't know what is inside "Caroussel_Img"
But if it is removing the file add "#" at beginning of the line and it won't bother you anymore
#unlink("uploads/".$img->path);

index.blade.php file not returning any view or scripts?

I'm facing an issue regarding my recent project, where i was trying to save some date through a form in the database (which is working as expected) but when i tried to fetch that data from my database it is not showing me any thing (as if code is not even there) in the view not even scripts but the same code is working in an other view.
Controller code:
public function index()
{
$products = Product::all();
return view('product.index',compact('products'));
}
Blade code:
#include('includes.header')
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12" style="margin: 30px" >
<table class="table table-bordered col-md-12 table-sm">
<thead>
<tr>
<th>ID</th>
<th>Image</th>
<th>Company Name</th>
<th>Title</th>
<th>Price</th>
<th>Status</th>
<th>Description</th>
<th>Created At</th>
<th>Updated At</th>
</tr>
</thead>
<tbody>
#if($products)
#foreach($products as $product)
<tr>
<td>{{$product->id}}</td>
<td><img src="" alt="">{{$product->photo ? $product->photo->file : 'no item photo'}}</td>
<td>{{$product->cname}}</td>
<td>{{$product->title}}</td>
<td>{{$product->price}}</td>
<td>{{$product->status == 1 ? "Active" : "Not Active"}}</td>
<td>{{$product->description}}</td>
<td>{{$product->created_at->diffForHumans()}}</td>
<td>{{$product->updated_at->diffForHumans()}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
</div>
</div>
<!-- /#page-wrapper -->
#include('includes.footer')
The main issue is it is behaving like the route is not defined and its view are not set (which is really not the case). I have defined it like
Route::resource('/admin/product','ProductController');
and it is returning me
how could that be possible if the routes are defined and it's not returning it's view.
I have created a custom route and view for it, and returned it's(index.blade.php) view to it and now it's working.

Redirect to the same page with retrieved values from database in laravel?

View Code:
<li class="active"><a href="#tab_1" data-toggle="tab" id="versiontab"
onclick="window.location='{{ url("versiondb") }}'" >Version
Database</a></li>
This is my view code.This is also a tab .Onclicking the tab the url redirection works.
Route Code:
Route::get('versiondb','versiondbController#select');
Controllercode:
public function select()
{
$users=debModel::all();
return redirect()->back()->with($users);
}
Here it should fetches the values from db and redirecting back to the same page and display the retreived values in table .But i am getting an error here as $users undefined
Inside the Same view (specified above):
<table class="table" id="table">
#foreach($users as $users)
<thead>
<tr class="header">
<th valign="middle" width="3%">Versions</th>
<th valign="middle" width="2%">Supported versions</th>
<th valign="middle" width="10%">Release Date</th>
<th valign="middle" width="3%">Type</th>
<th valign="middle" width="10%">Description</th>
<th valign="middle" width="3%">Actions</th>
<th valign="middle" width="3%">Downloader</th>
<!--<th valign="middle" width="3%">Beta code</th>-->
</tr>
</thead>
<tbody>
<tr>
<td>{{$users->versions}}</td>
<td></td>
</tr>
#endforeach
</tbody>
</table>
I think this is the place where error occurs.
When you use "->with" in Laravel, Laravel go back to that page with a session message named with.
Usiing with in your scenario would be
public function select()
{
$users=debModel::all();
return redirect()->back()->with("message","I generated all users");
}
which would be accessed in your blade as
#if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
The solution to your question is write it this way
public function select()
{
$data['users']=debModel::all();
return view("viewdeb",$data);
}
then your blade you will need a foreach loop
#foreach ($users as $user)
{{$user->firstname}}
#endforeach
mind you, more data can be sent back to the view by just including them in the data array
public function select()
{
$data['users']=debModel::all();
$data['business']=businessModel::all();
return view("viewdeb",$data);
}
The issue is here:
#foreach($users as $users)
here your array variable name and the variable in which you are holding the data on each iteration is the same, so change it to:
#foreach($users as $user)
<td>{{$user->versions}}</td>
Use ($users as $user) instead ($users as $users) here
#foreach($users as $user)
And in your template
<td>{{$user->versions}}</td>
Also change in your controller
return view('haghwayUpdate')->with('users',$users);

laravel-excel not working to export xls file using blade with image

Excuse me,
I want to use laravel-excel to export my report.
This is the controller:
public function getExcelfile(){
$users = UserService::findAllPublic();
$total = UserService::count();
$total_with_photo = UserService::countWithPhoto();
Excel::create('excelfile', function($excel) use ($users, $total, $total_with_photo) {
$excel->sheet('Excel', function($sheet) use ($users, $total, $total_with_photo) {
$sheet->loadView('report.excel')->with("users", $users)->with("total", $total)->with("total_with_photo", $total_with_photo);
});
})->export('xls');
}
This is the excel.blade.php located at report folder:
<html> <body> Total Registered User : {{$total}} Total Registered User With Photo : {{$total_with_photo}} <h1>Data User</h1> #if(!empty($users)) <table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Photo</th>
<th>Nama</th>
<th>Struck</th>
<th>Email</th>
<th>Phone</th>
<th>FB ID</th>
<th>Timestamp</th>
<th>Publish</th>
</tr>
</thead>
#foreach ($users as $row)
<tr>
<td>{{$row->id}}</td>
<td><img src="{{URL::asset('assets/images/upload/' . $row->photo)}}" width="200px"/></td>
<td>{{$row->nama}}</td>
<td>{{$row->struck}}</td>
<td>{{$row->email}}</td>
<td>{{$row->phone}}</td>
<td>{{$row->fbid}}</td>
<td>{{$row->timestamp}}</td>
<td>{{$row->publish}}</td>
</tr>
#endforeach </table> #endif </body> </html>
Then this error appears:
PHPExcel_Exception
File http://myserver.com/projectname/public/assets/images/upload/DSCN1164.jpg not found!
That file is exist when I try to access it at browser.
Instead of:
<img src="{{URL::asset('assets/images/upload/' . $row->photo)}}"
you should use:
<img src="assets/images/upload/{{{ $row->photo }}}"
Of course assuming it's correct path

Categories