【问题标题】:Laravel storage disk put gives internal server error 500 on live server | no logs are createdLaravel 存储磁盘 put 在 live 服务器上出现内部服务器错误 500 |没有创建日志
【发布时间】:2020-02-17 09:24:16
【问题描述】:

我正在将图像文件保存到磁盘。之后,我创建一个 ZIP 并下载它。在 localhost 上一切正常。

在我得到的实时服务器上:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

如果我想将图像保存到存储中,则会发生错误:

$user = DB::connection('mysql_live')->table('users')->where('id', $userid)->first();
Storage::disk('gdpr')->put($user->picture, file_get_contents(env('AVATAR_PHOTO_SRC') . $user->picture));

env('AVATAR_PHOTO_SRC')是图片的绝对路径,例如:https://example.com/uploads/thumb/


没有创建日志文件。而且压缩包的大小不到 1MB,图片更小。

这里发生了什么?

编辑

$user->picture 是文件名。例如:example.jpg

该文件确实存在并且可以在浏览器中打开。请记住,在 localhost 上一切正常。

编辑

我找到了一个登录try,catch:

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

编辑

无法在共享服务器上修改 PHP 初始化。我需要另一种方法来存储图像。

【问题讨论】:

  • 什么是$user->picture?是路径吗? env('AVATAR_PHOTO_SRC') . $user->picture) 上是否真的存在图像?您可能还想尝试/捕获 put 语句。
  • @QumberRizvi 请查看我的编辑
  • @QumberRizvi 我用 try catch 找到了一些东西,请参阅我的更新。看来我不能以这种方式存储图像,因为我无法更改服务器配置。
  • 请编辑您的 php.ini 以允许 url_fopen:allow_url_fopen = 1 allow_url_include = 1

标签: php laravel image download storage


【解决方案1】:

来自 PHP:www.php.net/manual/en/filesystem.configuration.php

allow_url_fopen 布尔值

此选项启用支持访问的 URL 感知 fopen 包装器 URL 对象,如文件。为访问提供了默认包装器 使用 ftp 或 http 协议的远程文件,一些扩展,如 zlib 可以注册额外的包装器。

因此您可能需要在 php.ini 中启用 allow_url_fopen=1 以启用 ftp 或 http 协议。

【讨论】:

  • 我知道这一点。将关闭问题。我需要 file_get_contents 的替代方法。 PHP ini 不能在共享服务器上修改。我需要另一种方法来存储图像。
  • 您可能想与您的托管服务提供商联系。如果你问得好,他们有时会允许这些操作。 :)
  • @Roman 有一个很好的库可以处理图像,请参阅:docs.spatie.be/laravel-medialibrary/v7/introduction
【解决方案2】:

这是对我有用的 curl 解决方案:

$c = curl_init();
curl_setopt($c, CURLOPT_URL, env('AVATAR_PHOTO_SRC') . $user->picture);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, 0);

curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($c, CURLOPT_CAINFO, Storage::disk('other')->path("cacert-2020-01-01.pem")); 
$img = curl_exec($c);

$httpCode_post = curl_getinfo($c, CURLINFO_HTTP_CODE);
$cerror_post = curl_error($c);

curl_close($c); 

Storage::disk('gdpr')->put($user->picture, $img);

您可以将CURLOPT_SSL_VERIFYPEER 设置为false,那么您不需要证书的步骤。

但我建议您添加证书。它超级容易。只需下载它here 并将其保存在您的存储空间中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 2015-10-11
    • 2014-10-25
    • 2015-08-12
    相关资源
    最近更新 更多