Unable to open file for reading [ file link ] laravel

3.4k views Asked by At

I am trying to send a mail to multiple recipients with an attachment of file URL , but while hitting the api in postman it's throwing an error unable to open file for reading [ file link ] , but while I am copying file link and opens in browser it's opening perfectly .

I have checked the file permission also and referred to some of the answers on Stackoverflow but nothing helped me, please help me as soon as possible.

enter image description here

        $file_name = 'TimeActivityReport' . "_" . time() . '.pdf';
        $storage_path = 'public/TimeActivityReport';
        // $storage_path = public_path();
        
        $filePath = $storage_path . '/' . $file_name;
        // return $filePath;
        $exl = Excel::store(new TimeActivityReportExport($all_total_values,$data,$date_totals), $filePath);
        if($exl)
        {
        $fileurl = asset('storage/TimeActivityReport').'/'.$file_name;
        // return $fileurl;
        }
// return $fileurl;
       return Mail::send([], $emails, function($message)use($fileurl,$emails) {
            $message->to($emails,'hello')
            ->subject('test')
            ->attach($fileurl,[
                'as' => 'checkname.pdf', 
                'mime' => 'application/pdf'
            ])
            ->setBody('check');
            });
1

There are 1 answers

4
Jelly Bean On BEST ANSWER

Try this I tested it on my end and it returned the file

Storage::get('./public/TimeActivityReport/'.$file_name);

You can also test if the file exists using:

Storage::disk('local')->exists('public/TimeActivityReport/'.$file_name);

To attach try:

$fileurl = Storage::path('public/TimeActivityReport/'.$file_name);

resource laravel docs