Getting partial content of a remote file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Getting partial content of a remote file

Post by LDusan »

Is it possible to download only partial content of a file with file_get_contents?
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: Getting partial content of a remote file

Post 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);
 
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting partial content of a remote file

Post by LDusan »

Great, I read on php.net something but they didn't give the specific example. I guess 0 is the starting point?
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: Getting partial content of a remote file

Post by genconv »

Yes, that's right.
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting partial content of a remote file

Post by LDusan »

It gives me "stream does not support seeking". Thanks anyway...
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: Getting partial content of a remote file

Post 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?
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting partial content of a remote file

Post by LDusan »

I am using preg_match on that file.

Again, the same with FILE_TEXT...
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: Getting partial content of a remote file

Post 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 ) )
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting partial content of a remote file

Post 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;
?>
 
User avatar
genconv
Forum Commoner
Posts: 34
Joined: Sun Jul 05, 2009 9:27 am

Re: Getting partial content of a remote file

Post 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;
?>
 
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Getting partial content of a remote file

Post by LDusan »

Hey thanks a lot for that!
Post Reply