错误异常:file_get_contents():https://wrapper在更改为allow_url_fopen=



在laravel项目中将SVG文件附加到电子邮件标记文件时出现此错误。

<img src="data:image/svg+xml;base64,{{ base64_encode(file_get_contents("../qrcodes/".$id.".svg")) }}" class="qrcode">

我添加

allow_url_fopen=1
allow_url_include=1

在PHP INI编辑器中。

而我仍然面临着这个错误。

ErrorException: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0

我该怎么解决。

在共享托管中,除非根用户自己启用,否则出于安全原因,像allow_url_fopen这样的php.ini值在服务器范围内被禁用。即使您在共享托管中提到allow_url_fopen=1,它也不会起作用。替代方案是使用cURL而不是file_get_contents

试试这个:

<?php
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
<img src="data:image/svg+xml;base64,{{ base64_encode(file_get_contents_curl("../qrcodes/".$id.".svg")) }}" class="qrcode">

如果PHP代码不打算直接粘贴在这里,那么您可以将此函数复制到functions.php中。

最新更新