简体   繁体   English

在视图中从公用文件夹访问资产(Laravel)

[英]Accessing assets from public folder,inside a view(Laravel)

I cannot access assets in my view. 我无法访问资产。

Here is my code. 这是我的代码。

 $mail->addAttachment(asset($file), 'receiptFile', 'base64', 'application/octet-stream');

The browser shows the following error: 浏览器显示以下错误:

Could not access file: http://localhost/receiptGenerator2/public/5a46187d954de.png Message could not be sent.Mailer Error: Could not access file: http://localhost/receiptGenerator2/public/5a46187d954de.png ------ 无法访问文件: http://localhost/receiptGenerator2/public/5a46187d954de.png邮件错误:无法访问文件: http://localhost/receiptGenerator2/public/5a46187d954de.png ----- -

As you can see the path to the file was successfully located. 如您所见,文件路径已成功定位。

If Laravel can find the file,why is it showing the error. 如果Laravel可以找到文件,为什么会显示错误。

Please try that way Use asset and give image name and if file is any folder than give {{ asset('folder_name/image_name') }} 请尝试使用“使用资产”并指定图像名称,如果文件是除{{ asset('folder_name / image_name')}}以外的任何文件夹,请尝试

{{ asset('5a46187d954de.png') }}

Its generate http://localhost/receiptGenerator2/public/5a46187d954de.png 其生成http://localhost/receiptGenerator2/public/5a46187d954de.png

Try this: 尝试这个:

->attach(
   public_path('receiptGenerator2/public/5a46187d954de.png'), [
   'as' =>"attachment.png" ,
   'mime' => 'application/octet-stream',
]);

Source: https://laravel.com/docs/5.5/mail#attachments 资料来源: https : //laravel.com/docs/5.5/mail#attachments

Edit: Since you're using phpmail this should do the trick 编辑:由于您使用的是phpmail,因此应该可以解决问题

 $mail->addAttachment(public_path($file), 'receiptFile', 'base64','application/octet-stream');

Mail requires the full path, yes you can open the file from that url from the browser but the phpmailer attempts to access http://localhost/receiptGenerator2/public/5a46187d954de.png in your disk, so it tries to get the file from /var/www/html/project/ http://localhost/receiptGenerator2/public/5a46187d954de.png which is not valid. Mail需要完整路径,是的,您可以从浏览器从该URL打开文件,但是phpmailer尝试访问磁盘中的http://localhost/receiptGenerator2/public/5a46187d954de.png ,因此它尝试从/获取文件var / www / html / project / http://localhost/receiptGenerator2/public/5a46187d954de.png无效。

As the docs for addAttachment() say, the first parameter must be a local file system path; 正如addAttachment()的文档addAttachment() ,第一个参数必须是本地文件系统路径; it will not work if you give it a URL. 如果您给它一个URL,它将不起作用。 PHPMailer isn't an HTTP client - there are many other functions and libraries to help with that, so use them if you need to fetch something from a remote URL to use as an attachment. PHPMailer不是HTTP客户端-还有许多其他功能和库可以帮助您解决此问题,因此,如果您需要从远程URL获取某些内容以用作附件,请使用它们。 You should not be using localhost URLs for local resources anyway as it's unnecessarily inefficient. 无论如何,您都不应使用本地主机URL作为本地资源,因为它不必要地效率低下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM