This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
uphp:functions:fwrite [2017/04/05 14:24] jeff |
uphp:functions:fwrite [2021/09/13 05:57] (current) |
||
---|---|---|---|
Line 7: | Line 7: | ||
====Description==== | ====Description==== | ||
- | <well size=" | + | <well size=" |
- | <span style=" | + | <span style=" |
- | < | + | <span style=" |
<span style=" | <span style=" | ||
<span style=" | <span style=" | ||
<span style=" | <span style=" | ||
<span style=" | <span style=" | ||
- | <span style=" | + | <span style=" |
+ | <span style=" | ||
+ | <span style=" | ||
+ | ) | ||
</ | </ | ||
- | 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 < | + | This function performs a binary-safe write of strings or bytes specified by integers to a previously opened resource((< |
====Parameters==== | ====Parameters==== | ||
Line 23: | Line 26: | ||
< | < | ||
- | < | + | < |
* < | * < | ||
- | * < | + | * < |
- | * < | + | * < |
- | ====Return Value==== | + | < |
- | **The number of bytes successfully written** | ||
- | Will return **0** if no bytes were written((A return value of 0 can happen for an invalid < | + | ====Return Values==== |
- | Will return **-1** for socket errors (reset, connection broken)((< | + | < |
+ | |||
+ | **0** if no bytes were written((< | ||
+ | |||
+ | **-1** for socket errors (reset, connection broken)((< | ||
====Examples==== | ====Examples==== | ||
Line 66: | Line 72: | ||
ABCDEF | ABCDEF | ||
Line 3 | Line 3 | ||
+ | </ | ||
+ | |||
+ | ====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. | ||
+ | |||
+ | For example, this script will make an exact copy of the contents of file original.bin into copy.bin: | ||
+ | <code php> | ||
+ | <? | ||
+ | $fo = fopen("/ | ||
+ | $fh = fopen("/ | ||
+ | $len = filesize($fo); | ||
+ | while ($len) { | ||
+ | if ($len> | ||
+ | | ||
+ | } else $buflen=$len; | ||
+ | $data = fread($fo, | ||
+ | // 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 | ||
+ | | ||
+ | $len = $len - $buflen; | ||
+ | } | ||
+ | fclose($fh); | ||
+ | fclose($fo); | ||
+ | ?> | ||
</ | </ | ||