WMPRO, WMMINI FW >= 1.0 WMMEGA FW >= 2.0
Write data to a file, stream or socket
This function performs a binary-safe write of strings or bytes specified by integers to a previously opened resource1)
$handle: Valid handle of a previously opened resource
$data: The string, int or array data to be written in one of the following forms:
$length: Optional length of buffer - automatically calculated for strings if not specified
Integer number of bytes successfully written
0 if no bytes were written4)
-1 for socket errors (reset, connection broken)5)
<? $fh = fopen("/fwrite_test.txt","w"); if (!$fh) { print("File open failed"); } else { $result =fwrite($fh,"Line 1"); $result+=fwrite($fh,13); $result+=fwrite($fh,10); $result+=fwrite($fh,array(65,66,67,68,69,70,13,10,"Line 3",13,10)); print($result." bytes were written to the file"); fclose($fh); } ?>
The above example will display:
24 bytes were written to the file
And the above example will create the file /fwrite_test.txt containing:
Line 1 ABCDEF Line 3
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:
<? $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); ?>
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
fgets() - Return a single line from a file, stream or socket, with optional size limit
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