【问题标题】:file_get_contents() PHP function not working on windows server running apachefile_get_contents() PHP 函数在运行 apache 的 Windows 服务器上不起作用
【发布时间】:2018-01-11 17:34:45
【问题描述】:

所以我有一个运行 windows 的服务器,它运行 PHP 环境来运行博客网站。

现在以下代码在我的本地主机上使用 xampp 运行良好,但在 windows 服务器上不起作用。

$image = get_the_post_thumbnail_url($postId, array(150,150));
$ext = pathinfo($image, PATHINFO_EXTENSION);
$base64 = 'data:image/' . $ext . ';base64,' . base64_encode(file_get_contents($image));

代码从循环中获取帖子缩略图并创建一个 base64 图像,我在模糊图像技术中使用它(就像前两个图像 on this site 发生的那样)

当我回显$ext 和$image 变量时,我得到了数据,但是当我回显file_get_contents($image) 时,我什么也得不到。我可以确认文件确实存在。

echo $ext . ':' . $image;
echo '<br />' . file_get_contents($image);

我遇到了对allow_url_fopen 的建议,但是当我通过回显phpinfo() 进行检查时,它已启用

echo  phpinfo();

php 版本是 5.6,所以这也不是问题。

此外,当我为 windows 服务器打开 wordpress 调试时,我收到此错误:警告:file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known。

任何人都知道此 preblom 的其他潜在原因。 干杯

【问题讨论】:

  • $image 变量中是否有有效的 url?该地址实际上是否存在域?该错误通常意味着 URL 不存在或找不到。
  • @JonathanKuhn 感谢您的帮助。图像确实存在。当我从图像变量复制输出并粘贴到我的浏览器中时,它可以工作
  • getaddrinfo 是一个与 php 无关的系统函数,除了 php 像许多其他程序一样使用它。您可以从服务器 ping 域吗?
  • 如果你用谷歌搜索那个错误信息,就会出现很多堆栈问题。看起来他们中的大多数都指向 DNS 问题。这是一个看起来很受欢迎的:stackoverflow.com/questions/20064372/…
  • 不,不是 DNS 问题,请参阅我的回答。我看了那些答案,但无济于事。感谢@JonathanKuhn 和 Don'tPanic 的帮助:)

标签: php windows file base64


【解决方案1】:

天哪,所以我想通了。

windows服务器是一个测试站点,url为https://test.domain.com.au 子域也是如此。

所以当我更新我的代码以在 url 中用 www 替换测试时,它可以工作。

$base64 = 'data:image/' . $ext . ';base64,' . base64_encode(file_get_contents(str_replace('test', 'www', $image)));

因此,即使图像 url https://test.domain.com.au/blog/wp-content/uploads/2017/03/my-image-150x150.jpg 是一个有效的 url,并且在粘贴到浏览器地址栏中时正在显示图像,file_get_contents() 函数也不喜欢它。

不知道为什么会这样,因为测试站点是一个实时站点,所以就像在互联网上一样。

我希望这对其他人有帮助:)

【讨论】:

    猜你喜欢
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 2014-06-23
    • 2011-07-15
    相关资源
    最近更新 更多