file_get_contents(): stream does not support seeking / When was PHP behavior about this changed?

后端 未结 7 1618
醉酒成梦
醉酒成梦 2020-12-06 05:00

When was PHP behavior about this changed?

From which PHP version is it?


Warning: file_get_contents(): stream does not supp

相关标签:
7条回答
  • 2020-12-06 05:19

    I had the same issue on my page when I moved it from one system to another, I was able to change the simple_html_dom.php file by removing the offset reference (didn't cause any further problems for me).

    On line 75 of simple_html_dom.php:

    $contents = file_get_contents($url, $use_include_path, $context, $offset);
    

    I removed the reference to $offset:

    $contents = file_get_contents($url, $use_include_path, $context);
    

    No my page works fine. Not taking liability for anything else it breaks! :)

    0 讨论(0)
  • 2020-12-06 05:23

    See file_get_contents(): stream does not support seeking PHP

    You are working with a remote file. Seeking is only supported for local files.

    You probably need to copy the file to your local file system before using file_get_html. It should work fine on localhost.

    0 讨论(0)
  • 2020-12-06 05:23

    first, try to change simple_html_dom.php like

    • remove the offset parameter from file_get_contents(...) on line 75

    • OR set the offset to 0 in file_get_html func on line 70

    if still not works ??? like mine

    then it means you have the latest version of PHP and you need to download the latest version of simple_html_dom.php from https://sourceforge.net/projects/simplehtmldom/

    after that, it works for me on each machine and system

    0 讨论(0)
  • 2020-12-06 05:27

    You don't need to edit the vendor files. Just change your requests from:

    $html = HtmlDomParser::file_get_html( "https://www.google.com/");
    

    to:

    $html = HtmlDomParser::file_get_html( "https://www.google.com/", false, null, 0 );
    

    The problem is that the default offset used by Simple HTML DOM is "-1" when you want it to be "0". Luckily it accepts it as a parameter, which means you can change it easily without needing to change the Simple HTML DOM source.

    Note: This compatibility issue was fixed in v1.7+

    0 讨论(0)
  • 2020-12-06 05:33

    Set $offset = 0

    That is working!

    0 讨论(0)
  • 2020-12-06 05:42

    Others have shared the solution, but no one has shared why. I don't know specifically why this is different between PHP 7.0 & 7.1, but the PHP.net docs for this function say:

    Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.

    I can confirm that removing the offset parameter in file_get_contents on line 75 works for me and/or setting the offset to 0 in the file_get_html function on line 70 works too.

    I guess that the offset parameter was never meant to be used with non local files since:

    The offset where the reading starts on the original stream. Negative offsets count from the end of the stream.

    Hope this helps clear up any confusion. With external sources, it makes sense to start streaming from the beginning.

    0 讨论(0)
提交回复
热议问题