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/05/08 14:26]
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">fwrite ( <span style="color:black">fwrite (
Line 15: Line 14:
 <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((<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>. Both of these capabilities simplify programming in uPHP for the Wattmon: data to be written can be specified as an <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>))+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 31: Line 32:
   * <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>))   * <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**
Line 68: 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.1494253588.txt.gz · Last modified: 2021/09/13 05:56 (external edit)