How can I download fIles - laravel

I created a page where I can upload files to "/storage/app/public/uploads". I have a download button, which works fine on my XAMPP test environment. As I uploaded the project to the server, I still can upload files. They also go to the same directory "/storage/app/public/uploads". But when I want to download them, the download-path is /storage/uploads/ and I get a 404 error. But this is also on my XAMPP.
in my /config/filesystems.php i have
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'links' => [
public_path('storage') => storage_path('app/public'),
],
]
I have no clue, what's wrong, because on my XAMPP it's fine. Any idea?

example can you create a new controller for example,
<a hrfe="{{route('downloadfile1')}}">download</a>
public function downloadfile1(){
$file=public_path()."/file1.pdf";
$headers = array(
'Content-Type: application/pdf',
);
return Response::download($file, 'filename.pdf', $headers);
}

Run php artisan storage:link on server to fix it.

Related

Laravel 6- Mail not sending from localhost

It showing me this error
Expected response code 250 but got code "530", with the message "530-5.7.0 Authentication Required. Learn more at 530 5.7.0 https://support.google.com/mail/?p=WantAuthError
while I have enabled two-factor authentication and generated an app password.
here is my .env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail#gmail.com
MAIL_PASSWORD=myAppGeneratedPassword
MAIL_ENCRYPTION=tls
and here is my mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'myemail#gmail.com'),
'name' => env('MAIL_FROM_NAME', 'my_name'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('myemail#gmail.com'),
'password' => env('appPassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/mail'),
],
],
];
Please help me. I am trying for three days.
Can you try with this config
MAIL_PORT=465
MAIL_ENCRYPTION=ssl

Cannot send message without a sender address Error in laravel

I'm trying to send raw mail but I;m getting an error "Cannot send message without a sender address" and I don't seem to see where I'm going wrong. I've provided the sender email in the .env file but it's still giving that error
My Controller!
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;
class ContactController extends Controller
{
public function show()
{
return view('user.contact');
}
public function store()
{
request()->validate(['email' => 'required|email']);
Mail::raw('it works', function ($message) {
$message->to(request('email'))
->subject('Hi There');
});
return redirect()->back();
}
}
My .env file
MAIL_MAILER=log
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=admin#example.com
MAIL_FROM_NAME="${APP_NAME}"
My mail.php file
<?php
return [
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
My form!
<form action="{{ route('contact')}}" method="post">
#csrf
<div class="form-group">
<label>E-mail</label>
<input type="email" name="email" class="form-control">
#error('email')
<p style="color: red; font-size: 15px; margin-top: 5px">{{ $message }}</p>
#enderror
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
I'm trying to send raw mail but I;m getting an error
Cannot send message without a sender address
and I don't seem to see where I'm going wrong. I've provided the sender email in the .env file but it's still giving that error
In your env file your username and password are null, you should be able to take the username and password from mailtrap, and it should work
Try this, it should work :
public function store(Request $request)
{
$this->validate($request, [
'email' => 'required|email'
]);
$email = $request->email;
$data = [
'email' => $request->email,
];
Mail::send('mail', $data, function ($message) use ($email) {
$message->to('myemail#gmail.com', 'Email Title')->subject('Test Subject 2');
$message->from($email, "Sender Name");
});
echo "HTML Email Sent. Check your inbox.";
}
When the configuration is cached the .env file no longer gets loaded. Instead Laravel uses the cached configuration so that the app loads quicker.
So when change .env file it is a good idea to run php artisan config:cache.
You need to clear cache in order to prevent this error . use the following commands one by one and your error will be solved .
- php artisan cache:clear
- php artisan route:cache
- php artisan view:clear
- php artisan config:cache
Hope it helped you .

Laravel PNG image shows but GIF image doesn't?

I have the following files in the public folder:
public/test.png
public/test.gif
In config/filesystems
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
The PNG file shows, the GIF doesn't (I get a 404). I ran php artisan storage:link and everything else is working fine, just the GIF files are not showing.
Make sure the path is correct. Names should be case sensitive (image.gif is not the same with image.Gif)

Laravel multi-tenant app works properly in local but does not work in production/staging

I build a multi-tenant app in laravel, That works properly in local but it does not work on production/staging. Whenever I try to login, it shows an error that : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tenancy.users' doesn't exist.
I have already run migrations and also try php artisan config:ca.
Package used : composer require "hyn/multi-tenant:5.3.*"
.env :
LIMIT_UUID_LENGTH_32=true
APP_URL_BASE=testdomain
APP_URL=http://${APP_URL_BASE}
DB_CONNECTION=system
TENANCY_HOST=127.0.0.1
TENANCY_PORT=3306
TENANCY_DATABASE=tenancy
TENANCY_USERNAME=root
TENANCY_PASSWORD=
database.php :
'default' => env('DB_CONNECTION', 'tenant'),
....
'system' => [
'driver' => 'mysql',
'host' => env('TENANCY_HOST', '127.0.0.1'),
'port' => env('TENANCY_PORT', '3306'),
'database' => env('TENANCY_DATABASE', 'tenancy'),
'username' => env('TENANCY_USERNAME', 'tenancy'),
'password' => env('TENANCY_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'tenant' => [
'driver' => 'mysql',
'host' => env('TENANCY_HOST', '127.0.0.1'),
'port' => env('TENANCY_PORT', '3306'),
'database' => env('TENANCY_DATABASE', 'tenancy'),
'username' => env('TENANCY_USERNAME', 'tenancy'),
'password' => env('TENANCY_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
.....
I also built a laraval mts, with laravel 8. It works locally and i have run into a similar issue.
ensure your .htaccess file is correct. Mine runs in cpanel and i found it was being overwritten everytime i updated the repo with master
make sure you have the databases set up. I use phpmyadmin on cpanel so i had to build the databases.
create a database user and give them access rights to the databases.
update .env to use your newly created database user

Laravel:Failed to retrieve image from storage

I have a function that store image to the storage/app/images
$filename = $image->getClientOriginalName();
if ($request->hasFile('image'))
{
$request->file('image')->storeAs('images', $filename);
}
$creator = new Content_form([
'media_id' => $request->get('media_name'),
'content_title' => $request->get('content_title'),
'content_description' => $request->get('content_description'),
'date_occured' =>\Carbon\Carbon::now()->format('Y-m-d'),
'page_number' => $request->get('page_number'),
'image' => $filename,
]);
$creator->save();
Image is stored in the database and in the storage/app/image only failed to retrieve those image here is my blade
#foreach($pendingNews as $number=> $post)
<td style="font-size: 16px; width: 210px"><img class="img-rounded" width="150" height="130" src="{{ asset('storage/app/images/'.$post->image) }}"></td>
#endforeach
Also file system I have
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
Remove /app from asset('storage/app/images/'.$post->image)

Resources