php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46043 file_get_contents() gives "stream does not support seeking" error with offset
Submitted: 2008-09-10 14:55 UTC Modified: 2008-09-10 23:32 UTC
From: penguin@php.net Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.2.6 OS: Linux, debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: penguin@php.net
New email:
PHP Version: OS:

 

 [2008-09-10 14:55 UTC] penguin@php.net
Description:
------------
file_get_contents() supports an offset. However, at certain times this gives an "stream does not support seeking" at a certain offset. This offset might differ with installations and the website a resource is requested from.



Reproduce code:
---------------
file_get_contents("http://stock.ter.dk/taske.jpg", null, null, 8000, 1000);

Expected result:
----------------
The content should be returned

Actual result:
--------------
Warning: file_get_contents(): stream does not support seeking in Command line code on line 1

Warning: file_get_contents(): Failed to seek to position 8000 in the stream in Command line code on line 1



The exact cutoff-point seem to differ. A script such as the following gives different results at different servers.

<?php
$offset = 0;
$length = 500;
while ($file = file_get_contents("http://stock.ter.dk/taske.jpg", null, null, $offset, $length)) {
  print "$offset, $length\n";
  $offset += $length;
}
?>

PHP 5.2.6-0.dotdeb.1 with Suhosin-Patch 0.9.6.2 (cli):

0, 500
500, 500
1000, 500
1500, 500
2000, 500
2500, 500
3000, 500
3500, 500
4000, 500
4500, 500
5000, 500
5500, 500
6000, 500
6500, 500
7000, 500
7500, 500

Warning: file_get_contents(): stream does not support seeking in /home/penguin/- on line 4

Warning: file_get_contents(): Failed to seek to position 8000 in the stream in /home/penguin/- on line 4

PHP 5.2.0 (cli) (built: Nov 26 2006 10:25:07):

0, 500
500, 500
1000, 500

Warning: file_get_contents(): stream does not support seeking in /home/penguin/- on line 4

Warning: file_get_contents(): Failed to seek to position 1500 in the stream. in /home/penguin/- on line 4



The behaviour is the same whether php-cli or mod_php is used, as seen at http://stock.ter.dk/filegetbug.php


(as a side note, file_get_contents() [or rather, the http stream] could try to use a ranged request when an offset and range is present, with a fallback to the current behaviour if the webserver returns the whole file)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-10 23:11 UTC] iliaa@php.net
You cannot seek on non-local files.
 [2008-09-10 23:32 UTC] penguin@php.net
But it does work fine (and returning the correct data) with a lower offset?

The provided code also shows that the error only occurs at some cutoff point, meaning the stream supports seeking without any problems until the limit.

I suppose this "it sometimes works" nature should be noted in the documentation or at least disallow file_get_contents() from using offset at all at a stream that does not support fseek().

Currently it is in a state where the feature could work one day and not the next (I suppose if the content is found within the first chunk of the request of a given limit) where it is not clear why the feature is not working.

At least the text "stream does not support seeking" is confusing as some kind of offset usage is possible to a certain limit.
 [2011-01-26 21:38 UTC] philip@php.net
Automatic comment from SVN on behalf of philip
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=307767
Log: - Document that the offset parameter does not work with remote files
- Mentioned when file_get_contents() emits E_WARNING errors
- Closes PHP Bug #53790
- Deals with PHP Bug #46043
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 04 08:01:28 2024 UTC