PHP Files
Writing to Many Filehandles Simultaneously
Problem
You want to send output to more than one filehandle; for example, you want to log messages to the screen and to a file.
Solution
Wrap your output with a loop that iterates through your filehandles:
function multi_fwrite($fhs,$s,$length=NULL) {
if (is_array($fhs)) {
if (is_null($length)) {
foreach($fhs as $fh) {
fwrite($fh,$s);
}
} else {
foreach($fhs as $fh) {
fwrite($fh,$s,$length);
}
}
}
}
$fhs = array();
$fhs['file'] = fopen('log.txt','w') or die($php_errormsg);
$fhs['screen'] = fopen('php://stdout','w') or die($php_errormsg);
multi_fwrite($fhs,'The space shuttle has landed.');
Discussion
If you don’t want to pass a length argument to fwrite() (or you always want to), you can eliminate that check from your multi_fwrite(). This version doesn’t contain a $length argument:
function multi_fwrite($fhs,$s) {
if (is_array($fhs)) {
foreach($fhs as $fh) {
fwrite($fh,$s);
}
}
}
No comments:
Post a Comment