User Tools

Site Tools


uphp:functions:fwrite

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:fwrite [2017/04/10 13:44]
jeff
uphp:functions:fwrite [2021/09/13 05:57] (current)
Line 7: Line 7:
 ====Description==== ====Description====
  
-<well size="sm"> +<well size="sm"><html>
-<html>+
 <span style="font-size:125%;color:green">int <span style="font-size:125%;color:green">int
-<span style="color:black"> +<span style="color:black">fwrite (
-</html> +
-[[fwrite]] ( +
-<html>+
 <span style="color:green">int <span style="color:green">int
 <span style="color:blue">$handle<span style="color:black">, <span style="color:blue">$handle<span style="color:black">,
 <span style="color:green">mixed <span style="color:green">mixed
 <span style="color:blue">$data <span style="color:blue">$data
-<span style="color:black">+<span style="color:black">[, 
-</html> +<span style="color:green">int 
-</well>+<span style="color:blue">$length<span style="color:black">
 +
 +</html></well>
  
-This function performs a binary-safe write of strings or bytes specified by integers to a previously opened resource((This function is similar to the mainline PHP function, but with the addition of the ability to write bytes specified as an <html><b><span style="color:green">integer<span style="color:black"></b></html>, and the ability to write <html><b><span style="color:green">string<span style="color:black"></b></html> or <html><b><span style="color:green">integer<span style="color:black"></b></html> data from an <html><b><span style="color:green">array<span style="color:black"></b></html>. Both of these capabilities simplify programming in uPHP for the Wattmon: data to be written can be specified as an <html><b><span style="color:green">int<span style="color:black"></b></html> byte value instead of a string, or a series of writes of either type can be combined into one statement by the use of an <html><b><span style="color:green">array<span style="color:black"></b></html>.))+This function performs a binary-safe write of strings or bytes specified by integers to a previously opened resource((<html>This function is similar to the mainline PHP function, but with the addition of the ability to write bytes specified as an <b><span style="color:green">integer<span style="color:black"></b>, and the ability to write <b><span style="color:green">string<span style="color:black"></b> or <b><span style="color:green">integer<span style="color:black"></b> data from an <b><span style="color:green">array<span style="color:black"></b></html>. Both of these capabilities simplify programming in uPHP for the [[hardware:wattmons|Wattmon]]: data to be written can be specified as an <html><b><span style="color:green">int<span style="color:black"></b> byte value instead of a string, or a series of writes of either type can be combined into one statement by the use of an <b><span style="color:green">array<span style="color:black"></b>.</html>))
  
 ====Parameters==== ====Parameters====
Line 28: 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">$data<span style="color:black"></b></html>:  The <html><b><span style="color:green">string<span style="color:black"></b></html>, <html><b><span style="color:green">int<span style="color:black"></b></html> or <html><b><span style="color:green">array<span style="color:black"></b></html> data to be written in one of the following forms:+<html><b><span style="color:blue">$data<span style="color:black"></b>:  The <b><span style="color:green">string<span style="color:black"></b>, <b><span style="color:green">int<span style="color:black"></b> or <b><span style="color:green">array<span style="color:black"></b> data to be written in one of the following forms:</html>
  
   * <html><b><span style="color:green">string<span style="color:black"></b></html> - A string to write   * <html><b><span style="color:green">string<span style="color:black"></b></html> - A string to write
-  * <html><b><span style="color:green">int<span style="color:black"></b></html> - A single byte to write specified as an integer (8 bits, decimal 0-255)((If <html><b><span style="color:blue">$data<span style="color:black"></b></html> is type <html><b><span style="color:green">int<span style="color:black"></b></html> then 1 byte will be written to the resource, even if the <html><b><span style="color:green">integer<span style="color:black"></b></html> is not in the range 0 to 255 (for which there may be unexpected results). For example, <html><b><span style="color:green">integer<span style="color:black"></b></html> '321' is equivalent to writing '65' which will write the character 'A' to the resource (321 modulo 256 = 65). If <html><b><span style="color:blue">$data<span style="color:black"></b></html> is type <html><b><span style="color:green">float<span style="color:black"></b></html> then 0 bytes will be written.)) +  * <html><b><span style="color:green">int<span style="color:black"></b></html> - A single byte to write specified as an integer (8 bits, decimal 0-255)((<html>If the <b><span style="color:blue">data<span style="color:black"></b> is type <b><span style="color:green">int<span style="color:black"></b> then 1 byte will be written to the resource, even if the <b><span style="color:green">integer<span style="color:black"></b> is not in the range 0 to 255 (for which there may be unexpected results). For example, <b><span style="color:green">integer<span style="color:black"></b> '321' is equivalent to writing '65' which will write the character 'A' to the resource (321 modulo 256 = 65). If the <b><span style="color:blue">data<span style="color:black"></b> is type <b><span style="color:green">float<span style="color:black"></b> then 0 bytes will be written.</html>)) 
-  * <html><b><span style="color:green">array<span style="color:black"></b></html> - A series of <html><b><span style="color:green">string<span style="color:black"></b></html> and/or <html><b><span style="color:green">int<span style="color:black"></b></html> byte data to write - useful for writing several strings and/or binary data to a resource((If <html><b><span style="color:blue">$data<span style="color:black"></b></html> is type <html><b><span style="color:green">array<span style="color:black"></b></html> and the array contains integers that are not in the range from 0 to 255 (8 bits) then multiple bytes will be written (up to 4 bytes or 32 bits, which may be unexpected). Array elements that are type <html><b><span style="color:green">float<span style="color:black"></b></html> or <html><b><span style="color:green">array<span style="color:black"></b></html> will always write 0 bytes.))+  * <html><b><span style="color:green">array<span style="color:black"></b> - A series of <b><span style="color:green">string<span style="color:black"></b> and/or <b><span style="color:green">int<span style="color:black"></b> byte data to write - useful for writing several strings and/or binary data to a resource</html>((<html>If the <b><span style="color:blue">data<span style="color:black"></b> is type <b><span style="color:green">array<span style="color:black"></b> and the array contains integers that are not in the range from 0 to 255 (8 bits) then multiple bytes will be written (up to 4 bytes or 32 bits, which may be unexpected). Array elements that are type <b><span style="color:green">float<span style="color:black"></b> or <b><span style="color:green">array<span style="color:black"></b> will always write 0 bytes.</html>))
  
-====Return Value====+<html><b><span style="color:blue">$length<span style="color:black"></b></html>:  Optional length of buffer - automatically calculated for strings if not specified 
 + 
 + 
 +====Return Values====
  
 <html><b><span style="color:green">Integer<span style="color:black"></b></html> **number of bytes successfully written** <html><b><span style="color:green">Integer<span style="color:black"></b></html> **number of bytes successfully written**
  
-**0** if no bytes were written((A return value of 0 can happen for an invalid <html><b><span style="color:blue">$handle<span style="color:black"></b></html>, an empty <html><b><span style="color:green">string<span style="color:black"></b></html>, or if <html><b><span style="color:blue">$data<span style="color:black"></b></html> is type <html><b><span style="color:green">float<span style="color:black"></b></html>.))+**0** if no bytes were written((<html>A return value of 0 can happen for an invalid <b><span style="color:blue">handle<span style="color:black"></b>, an empty <b><span style="color:green">string<span style="color:black"></b>, or if the <b><span style="color:blue">data<span style="color:black"></b> is type <b><span style="color:green">float<span style="color:black"></b>.</html>))
  
 **-1** for socket errors (reset, connection broken)((<badge>FW < 1.1019</badge> returned **0** for socket errors.)) **-1** for socket errors (reset, connection broken)((<badge>FW < 1.1019</badge> returned **0** for socket errors.))
Line 71: Line 72:
 ABCDEF ABCDEF
 Line 3 Line 3
 +</code>
 +
 +====Note====
 +
 +When working with binary data such as data read from a file using fread(), make sure to pass the variable as a pointer using the & symbol.  Otherwise a copy of the string will be made but will only be copied until the first NULL character is found.
 +
 +For example, this script will make an exact copy of the contents of file original.bin into copy.bin:
 +<code php>
 +<?
 +  $fo = fopen("/original.bin","r");
 +  $fh = fopen("/copy.bin","w");
 +  $len = filesize($fo);
 +  while ($len) {
 +     if ($len>512) {
 +       $buflen=512;
 +     } else $buflen=$len;
 +     $data = fread($fo,512);
 +     // make sure you also specify the optional file size 
 +     // or else it will use the length of the string (till the first null).
 +     // also, pass the variable by reference
 +     fwrite($fh,&$data,512);
 +     $len = $len - $buflen;
 +  }
 +  fclose($fh);
 +  fclose($fo);
 +?>
 </code> </code>
  
uphp/functions/fwrite.1491831891.txt.gz · Last modified: 2021/09/13 05:56 (external edit)