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.
Related
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.
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);
Mu Controller Code:
if ($model->load(Yii::$app->request->post())) {
$data = $this->renderPartial('receivers/contact_form_output_quick_query', [
'model' => $model,
'formData' => $_REQUEST], true);
$receiver_message = $this->renderPartial('receivers/receiver_message_quick_query',['data' => $data]);
$message = Yii::$app->mailer->compose();
$message->setFrom('info#company.com')
->setTo('info#company.com')
->setSubject('Subject Of email')
->setHtmlBody($data)
->send();
$message = Yii::$app->mailer->compose();
$message->setFrom('info#company.com')
->setTo($model->email)
->setSubject('Reply: Online Query')
->setHtmlBody($receiver_message)
->send();
Yii::$app->session->setFlash('success', 'Congratulations! Your message has been successfully Sent.');
}
Will some one tells me what is the main reason that image and Font Awesome icons are not sending properly?
My receiver message partial view goes here:
<div>
<table style="width: 600px;">
<tr>
<td>Thanks for your interest in our services. We have received your online query.</td>
</tr>
<tr>
<td>Thanks again for contacting us.</td>
</tr>
<tr>
<td style="height: 5px;"></td>
</tr>
<tr>
<td>(It may take some time depending upon the number of queries in the pipeline. We appreciate your cooperation in this regard.)</td>
</tr>
<tr>
<td>Regards!</td>
</tr>
</table>
<?= $this->render('signature'); ?>
<table>
<tr>
<td><strong style="font-size:16px;">Query Details that we received:</strong></td>
</tr>
<tr>
<td><?= $data; ?></td>
</tr>
</table>
</div>
The above is the render partial view for the receiver message:
My signature partial view is:
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<table>
<tr>
<td colspan="3">
<img src="<?= $this->theme->baseUrl?>/images/email_signature.png">
</td>
</tr>
<tr>
<td style="height: 30px;"></td>
</tr>
<tr>
<td>+123456778</td>
<td colspan="2"> | +123456778</td>
</tr>
<tr>
<td>http://www.example.com</td>
<td>| info#company.com </td>
<td>| <b>Skype:</b> skypeid</td>
</tr>
<tr>
<td><a target="_blank" href="https://www.linkedin.com"><i class="fa fa-linkedin"></i> Linked In | </a></td>
<td><a target="_blank" href="https://www.facebook.com"><i class="fa fa-facebook"></i> Facebook | </a></td>
<td><a target="_blank" href="https://twitter.com"><i class="fa fa-twitter"></i> https://www.twitter.com</a></td>
</tr>
The above is the render partial view for signature containing image and font awesome icons main problem is in this file.
Images
$this->theme->baseUrl will return path to theme, but without domain and scheme part (for example it will return /themes/my-theme). When you're generating URLs for mailing, you need to full URL with scheme and domain - you should generate URLs like this:
<img src="<?= Yii::$app->request->getHostInfo() . $this->theme->getBaseUrl() ?>/images/email_signature.png">
Font Awesome Icons
I have a bad news for you - many clients (especially webmails) does not support webfonts, so you will not be able to use Font Awesome icons in this way. Support for SVG is even worse, so this is not a solution either. You should probably stick to good old PNGs as icons.
Unfortunately working with mailing is like a time travel - you should build your emails in the same way you created your websites 10-20 years ago. Forget about fancy CSS3 features - the most bulletproof templates are still built using tables with inline CSS everywhere...
Not every mail client supports webfonts. So probably best for you is to download it as images, save it in your public assets folder and invoke it with img element tag.
if I am not wrong you need to use embed() or attach() for showing inline images in the email.
$Imageid = $message->attach(Swift_Attachment::fromPath('path/to/image')
->setDisposition('inline'));
and then use the $cid for showing the image inside the email text where ever you are assigning the text for the email
<img src="' . $Imageid . '" alt="Image" />'
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);
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