fread();

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
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

fread();

Post by scts102 »

I am using the fread function to get data from a remote server. The problem is, however, that I do not know the length of data being recieved.

How can I get the data from the remote server without knowing the length of the data?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

file_get_contents() -- Much simpler, one-step, no length needed ;)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Indeed.

Also, check out http://www.php.net/filesystem for more functions that relate to working with files. There is also many user comments there that are helpful.
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

Could you please give me an example of how to use that?
I tried it, but I got an error, something along the lines of expecting string resource.

here is what I tried:

Code: Select all

$fp = @fsockopen("tcp://".server($addr), $port, $errno, $errstr);
 $tmp= hlsend($fp, "\\version\\"); 
  $c = '';
  $c .= file_get_contents($tmp);
  //echo("Version:="); 
  echo($c);
I also tried the file_get_contents with $fp instead of $tmp....I am inexperienced with this sort of thing...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Firstly, is there a specific requirement of using sockets to get the data instead of only file_get_contents?
File_get_contents does not need filepoints, so does not need fsockopen().

Also whats that hlsend()?
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

Sorry, I should have explained better:

This is an extansion off of another topic (See: Query Server in PHP-Code)
Basically, a connection is opened to a server, and a command is sent to it. I need to get the server's response, and display it (A "query", if you will). the response will always be a simple string:no array of values. However, I dont know the length of the data being sent from the server, so using fRead would be difficult (for me anyway...maybe there is a workaround?)

hlsend is a function :

Code: Select all

function hlsend ($udp, $str) { 
    fwrite($udp, "${str}"); 
    fread($udp, 1); 
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

file_get_contents prob wont work on that then.

Try strlen() to get the length, or find the documentation for whatever protocol you're using ;)
Post Reply