【问题标题】:file_get_contents not working with variable PHPfile_get_contents 不适用于变量 PHP
【发布时间】:2020-11-24 08:42:47
【问题描述】:

我的 PHP 版本是 5.6.40。 我有一个变量,我想在其中存储一个图像 url。

$imageurl = "http://i132.photobucket.com/albums/q13/animalhelper2006/1376.jpg";

我正在使用file_get_contents 函数来获取这张图片。

$contents = file_get_contents($imageurl);

但它不起作用,它显示空结果。但是当我尝试直接加载图片网址时,它起作用了。

$contents = file_get_contents("http://i132.photobucket.com/albums/q13/animalhelper2006/1376.jpg");

怎么了?请帮忙。

谢谢

【问题讨论】:

  • 不可重现,正如预期的那样,我得到了相同的结果。
  • @CBroe 奇怪,第二种方法对我有用,但我当然需要使用变量。
  • 确保您的变量包含完全您认为应该包含的内容,对其进行 var_dump,并查看报告的字符串长度。确保其两侧没有额外的空格或其他不可打印的字符。
  • @CBroe 你是对的,我使用了 var_dump 并且看到长度不同。经过多次检查,我发现变量是十六进制代码 http://i132.photobucket.com/albums/q13/animalhelper2006/1376.jpg 最后我使用了 html_entity_decode 函数,它现在工作正常。非常感谢。

标签: php file-get-contents


【解决方案1】:

您的问题可能来自包含空格 (" ") 的文件名。 您可以在将其解析为 file_get_contents 之前尝试使用 urlencode 文件名。

【讨论】:

  • 谢谢,经过多次检查,我发现变量是十六进制代码 http://i132.photobucket.com/albums/q13/animalhelper2006/1376.jpg 最后我使用html_entity_decode 函数,它现在工作正常。
【解决方案2】:

按照@CBroe 的方式。我用过 var_dump ,看到长度不同。经过多次检查,我发现变量是十六进制代码http://i132.photobucket.com/albums/q13/animalhelper2006/1376.jpg 最后我使用了 html_entity_decode 函数,它现在工作正常。

$contents = file_get_contents(html_entity_decode($imageurl));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多