Skip to Content
PHP 5 Kochbuch, Third Edition
book

PHP 5 Kochbuch, Third Edition

by David Sklar, Adam Trachtenberg, Carsten Lucke, Matthias Brusdeylins, Ulrich Speidel, Stephan Schmidt
September 2009
Intermediate to advanced content levelIntermediate to advanced
912 pages
48h 11m
German
O'Reilly Verlag
Content preview from PHP 5 Kochbuch, Third Edition

21.18 An viele Datei-Handles gleichzeitig schreiben

Problem

Sie wollen eine Ausgabe an mehr als ein Datei-Handle schreiben; zum Beispiel wollen Sie Meldungen auf den Bildschirm und an eine Datei ausgeben.

Lösung

Packen Sie Ihre Ausgabe in eine Schleife, die Ihre Datei-Handles durchläuft, wie in Listing 21.4 gezeigt.

function pc_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);
      }
    }
  }
}

Listing 21.4 pc_multi_fwrite()

Hier kommt ein Beispiel:

$fhs['file'] = fopen('log.txt','w') or die($php_errormsg);
$fhs['screen'] = fopen('php://stdout','w') or die($php_errormsg);

pc_multi_fwrite($fhs,'Der Spaceshuttle ist gelandet.');

Diskussion ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP programmieren unter Windows

PHP programmieren unter Windows

Arno Hollosi

Publisher Resources

ISBN: 9783868993271Purchase book