User Tools

Site Tools


uphp:functions:fgets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
uphp:functions:fgets [2021/04/07 08:40]
admin
uphp:functions:fgets [2021/09/13 05:57] (current)
Line 1: Line 1:
-======fgets_long======+======fgets======
  
 <badge>WMPRO, WMMINI FW >= 1.0</badge> <badge>WMMEGA FW >= 2.0</badge> <badge>WMPRO, WMMINI FW >= 1.0</badge> <badge>WMMEGA FW >= 2.0</badge>
  
-Get string of arbitrary length from a file handle +Return single line from a file, stream or socket, with optional size limit
-====Library==== +
- +
-/lib/uphp/fileio.inc+
  
 ====Description==== ====Description====
  
 <well size="sm"><html> <well size="sm"><html>
-<span style="font-size:125%;color:green">string +<span style="font-size:125%;color:green">mixed 
-<span style="color:black">fgets_long (+<span style="color:black">fgets (
 <span style="color:green">int <span style="color:green">int
 <span style="color:blue">$handle <span style="color:blue">$handle
-)+<span style="color:black">[, 
 +<span style="color:green">int 
 +<span style="color:blue">$size 
 +<span style="color:black">)
 </html></well> </html></well>
  
-Since uPHP has a maximum length of 255 bytes for strings read from disk +This function works with different resource types. If no size is specified, it will keep reading from the filestream or socket until it reaches the end of the line (or the end of the file). For streams and sockets it will return characters until a line feed (LF) or carriage return (CR) is reached (it will wait until an entire line is received). If the optional size is specified and the line is longer than this size, the function will return only the number of characters specified. 
-this function checks for the condition and keeps reading till the end of  + 
-the string.+**Note:** The maximum length of a returned string in [[hardware:wattmons|Wattmon]] is 254 bytes so when reading a longer input stream, you will need to check the string length using [[strlen()]] and if it is equal to 254 you would need to perform additional reads to ensure that a full line is read from the input stream.
  
 ====Parameters==== ====Parameters====
Line 26: Line 26:
 <html><b><span style="color:blue">$handle<span style="color:black"></b></html>:  Valid handle of a previously opened resource <html><b><span style="color:blue">$handle<span style="color:black"></b></html>:  Valid handle of a previously opened resource
  
 +<html><b><span style="color:blue">$size<span style="color:black"></b></html>:  Optional limit to the number of bytes returned
  
 ====Return Values==== ====Return Values====
  
-  * <html><b><span style="color:green">String<span style="color:black"></b></html> containing the line of characters (without CR/LF)    +  * <html><b><span style="color:green">String<span style="color:black"></b></html> containing the line of characters (without CR/LF) 
 +    * Empty <html><b><span style="color:green">string<span style="color:black"></b></html> if there is no data on the line 
 +  * <html><b><span style="color:green">Integer<span style="color:black"></b></html> -1 for error
  
 ====Example==== ====Example====
 +
 +===Open a TCP connection to google.com and print the header for a search for the word 'test'===
  
 <code php> <code php>
 <pre><? <pre><?
-  include("/lib/uphp/fileio.inc"); +  $f=fsockopen("www.google.com",80,10);
-   +
-  $f = fopen("/logs/log.txt"); +
-  +
   if ($f) {   if ($f) {
-    $line=0; +    // send the request: 
-    while (!feof($f)) { +    $out "GET /search?q=test HTTP/1.0\r\n"
-      $st=fgets_long($f); +    $out = $out."Host: www.google.com\r\n"; 
-      print("Line ".(++$line)."=".$st);+    $out = $out."Connection: Close\r\n\r\n"; 
 +    fwrite($f,$out)
 +    print($out); 
 +    // wait for and receive the header (multiple lines): 
 +    $done=0; 
 +    while (!$done) { 
 +      $str=fgets($f); 
 +      print($str+"\r\n"); 
 +      if (strlen($str)==0$done=1;
     }     }
     fclose($f);     fclose($f);
   }   }
-   
 ?></pre> ?></pre>
 </code> </code>
  
 +====Also See====
 +
 +[[strlen()]] - Return the length of a <html><b><span style="color:green">string</b></html>
 +
 +[[fopen()]] - Open a file for reading or writing
 +
 +[[fseropen()]] - Open the serial port at the specified baud rate with optional parameters
 +
 +[[f485open()]] - Open the RS-485 port at the specified baud rate and parity
 +
 +[[fsockopen()]] - Open an internet socket connection with optional timeout
 +
 +[[fread()]] - Read bytes from a file, stream or socket
 +
 +[[fwrite()]] - Write data to a file, stream or socket
 +
 +[[feof()]] - Test if no more data is available in a file, stream or socket
 +
 +[[filesize()]] - Return the size of a file, or the number of unread bytes in a stream or socket
 +
 +[[fseek()]] - Position the file pointer in an open file
 +
 +[[ftell()]] - Return the current position of a file read/write pointer
  
 +[[fclose()]] - Close a file, stream or socket
  
uphp/functions/fgets.1617784833.txt.gz · Last modified: 2021/09/13 05:56 (external edit)