Page 1 of 1

Getting partial content of a remote file

Posted: Mon Jul 06, 2009 9:43 am
by LDusan
Is it possible to download only partial content of a file with file_get_contents?

Re: Getting partial content of a remote file

Posted: Mon Jul 06, 2009 11:07 am
by genconv
There is no problem. With the following code you can read first 200 characters of the remote file:

Code: Select all

 
<?php
$content = file_get_contents('http://www.google.com/', FILE_BINARY,  NULL, 0, 200);
 

Re: Getting partial content of a remote file

Posted: Mon Jul 06, 2009 12:50 pm
by LDusan
Great, I read on php.net something but they didn't give the specific example. I guess 0 is the starting point?

Re: Getting partial content of a remote file

Posted: Mon Jul 06, 2009 1:05 pm
by genconv
Yes, that's right.

Re: Getting partial content of a remote file

Posted: Mon Jul 06, 2009 2:55 pm
by LDusan
It gives me "stream does not support seeking". Thanks anyway...

Re: Getting partial content of a remote file

Posted: Tue Jul 07, 2009 12:07 am
by genconv
For me it works, but you can try to replace "FILE_BINARY" to "FILE_TEXT" anyway:

Code: Select all

 
<?php
$content = file_get_contents('http://www.google.com/', FILE_TEXT,  NULL, 0, 200);
 
if(strpos($content, 'charset') !== false){
    echo 'OK';
}
 
What function are you using to search the string?

Re: Getting partial content of a remote file

Posted: Tue Jul 07, 2009 5:53 am
by LDusan
I am using preg_match on that file.

Again, the same with FILE_TEXT...

Re: Getting partial content of a remote file

Posted: Tue Jul 07, 2009 7:38 am
by genconv
For me the following code works fine:

Code: Select all

 
<?php
$content = file_get_contents('http://www.google.com/', FILE_TEXT,  NULL, 0, 200);
$pattern = '/charset/';
preg_match($pattern, $content, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
 
Result:

Code: Select all

Array ( [0] => Array ( [0] => charset [1] => 79 ) )

Re: Getting partial content of a remote file

Posted: Tue Jul 07, 2009 8:40 am
by LDusan
Thanks for the effort. Where do I make mistake?

Code: Select all

 
 
<?php
$file3 = file_get_contents ("http://www.tportal.hr/funbox/astrofun/", FILE_TEXT, NULL, 26671, 40139);
$pattern="/horoskopDnevniTekst\W1\W(.+?)\;/";
if (preg_match ($pattern, $file3, $out))
$title = $out[1];
echo $title;
?>
 

Re: Getting partial content of a remote file

Posted: Tue Jul 07, 2009 9:03 am
by genconv
Unfortunately this is a PHP bug:(
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.

You can try this: (I replaced 26671 to 3000)

Code: Select all

 
<?php
$file3 = file_get_contents ("http://www.tportal.hr/funbox/astrofun/", FILE_TEXT, NULL, 3000, 40139);
$pattern="/horoskopDnevniTekst\W1\W(.+?)\;/";
if (preg_match ($pattern, $file3, $out))
$title = $out[1];
echo $title;
?>
 

Re: Getting partial content of a remote file

Posted: Wed Jul 08, 2009 3:36 pm
by LDusan
Hey thanks a lot for that!