Unable to open file for reading [ file link ] laravel - laravel

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

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

Related

Laravel - "file does not exist or is not readable", but the file is moved successfully

I get the follow error:
The "C:\xampp\tmp\php49D8.tmp" file does not exist or is not readable.
But the file is copied successfully
My controller code is:
$fileResult=$file->move(self::UPLOAD_DIR, $name_file);
if(!$fileResult){
$result = array("status" => "500",
"error"=> array("error" => "Error in the file move"));
return response(json_encode( $result ), $result["status"])
->header("Content-Type", "application/json");
}
Screenshot: here
Why can be the problem?
Call $validator->fails() can delete uploading file
use
$file = $request->file('file');//get your file
$fileResult=$file->move(self::UPLOAD_DIR, $file->getClientOriginalName());

The Provided CWD does not exist

In my application I allow a user to generate both pdf and xls reports and save in my s3 bucket. This has worked for a while now till today.
When I try generating, the xls generates fine but for the pdf I get this result:
Message The provided cwd "" does not exist.
Level ERROR
Line 334
File /var/app/current/vendor/symfony/process/Process.php
This is my function to generate pdf report:
protected function exportPdf($sales)
{
$folder = Carbon::now()->toDateString();
$pdf = \PDF::loadView("reports.pdf.sales",
[
"data" => $sales,
"report" => [
"title" => "Sales Report",
"date" => Carbon::now()->toDayDateTimeString()
]
])->setPaper('a4')
->setOrientation("portrait");
$guid = bin2hex(random_bytes(32));
Storage::put($folder . "/" . $guid . ".pdf", $pdf->output());
return "https://s3-us-west-2.amazonaws.com/xxxxxx/" . $folder . "/" . $guid . ".pdf";
}
In my env file I have setup my FILESYSTEM_DRIVER to point to s3 bucket:
FILESYSTEM_DRIVER=s3
I use the laravel/snappy package.
I have cleared cache, restarted my queues, run composer update but still the issue persists.

Laravel issue Credentials are required to create a Client

I need to test send SMS to mobile I get Credentials are required to create a Client error for My Code Here
.env
TWILIO_ACCOUNT_SID=AC15...................
TWILIO_AUTH_TOKEN=c3...................
TWILIO_NUMBER=+1111...
Config\App
'twilio' => [
'TWILIO_AUTH_TOKEN' => env('TWILIO_AUTH_TOKEN'),
'TWILIO_ACCOUNT_SID' => env('TWILIO_ACCOUNT_SID'),
'TWILIO_NUMBER' => env('TWILIO_NUMBER')
],
Controller
$accountSid = env('TWILIO_ACCOUNT_SID');
$authToken = env('TWILIO_AUTH_TOKEN');
$twilioNumber = env('TWILIO_NUMBER');
$client = new Client($accountSid, $authToken);
try {
$client->messages->create(
'0020109.....',
[
"body" => 'test',
"from" => $twilioNumber
// On US phone numbers, you could send an image as well!
// 'mediaUrl' => $imageUrl
]
);
Log::info('Message sent to ' . $twilioNumber);
} catch (TwilioException $e) {
Log::error(
'Could not send SMS notification.' .
' Twilio replied with: ' . $e
);
}
Twilio developer evangelist here.
A quick read over the environment config for Laravel suggests to me that you can use the env method within your config files, as you are doing, but it's not necessarily available in application code. Since you are committing your environment variables to the config object, I think you need to use the config method instead.
$accountSid = config('TWILIO_ACCOUNT_SID');
$authToken = config('TWILIO_AUTH_TOKEN');
$twilioNumber = config('TWILIO_NUMBER');
Let me know if that helps at all.

Laravel write file stream

I am using guzzle to downlaod file from url and save it into my storage.
So I have a code look like this
$response = $this->client->request('GET', $model->url, [
'stream' => true
]);
$body = $response->getBody();
while (!$body->eof()) {
Storage::append($this->filePath, $body->read(1024));;
}
but when I open the folder where my file is located, I see that the file size is changing and sometimes it is zero. So in the end I am getting a invalid file.
How can I solve this problem?

Laravel 5.3 response()->download - File(.doc, .docx) becomes unreadable after downloading

Laravel 5.3
When I download a file (.doc, .docx) from my storage folder it becomes unreadable. If I go to the local folder and open the file however it is valid and readable.
I am using the standard download function, using headers and stuff.. Have a look at my code:
$fileNameGenerate = 'example_filename';
$fileArr = [ 'wierd_filename', 'docx' ];
$cvPath = storage_path('app/example_folder/subfolder/wierd_filename.docx');
$headers = array(
'Content-Type: application/' . $fileArr[1],
);
try {
return response()->download($cvPath, $fileNameGenerate . '.' . $fileArr[1], $headers);
} catch (\Exception $e) {
//Error
return redirect()->back()->with('error', trans('locale.file_does_not_exists'));
}
Does anyone know what is wrong here? Thank you!
Update: I removed headers, it doesn't work with or without them.
Here is how the files render in the 2 different cases:
Try this
public function getDownload()
{
//doc file is stored under storagepath/download/info.docx
$file= pathofstorage. "/download/info.docx";
return response()->download($file);
}
I added:
ob_end_clean();
before:
response -> download
and it worked for me.

Resources