note 59885 added to function.fopen

From: Date: Mon, 19 Dec 2005 12:05:56 +0000
Subject: note 59885 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-100406@lists.php.net to get a copy of this message
I found a nice trick how to work around the issue (mentioned here: http://www.php.net/manual/en/function.fopen.php#41243) that the PHP process will block on opening a FIFO until data is sent to it: Simply do send some data to the FIFO, using an echo command started in background (and ignoring that spedific data, when parsing whatever read from the FIFO). A very simple example of that: <? // path & name of your FIFO-file $someFIFO = "path/to/your/fifo" // some string, that won't be found in your regular input data $uniqueData = "some specific data"; // this statement actually does the trick providing some data waiting in the FIFO: // start echo to send data to the FIFO in the background!! // NOTE: parenthesis & second redirection (to /dev/null) are // important to keep PHP from waiting for echo to terminate! system("(echo -n '$uniqueData' >$someFIFO) >/dev/null &"); // now you can safely open the FIFO, without the PHP-process being blocked $handle = fopen($someFIFO, 'r'); // loop reading data from the FIFO while (TRUE) { $data = fread($handle, 8192); // eliminate the initially sent data from our read input // NOTE: this is done only in a very simplyfied way in this example, // that will break if that data-string might also be part of your regular input!! if (!(strpos($inp, $uniqueData) === FALSE)) $data = str_replace($uniqueData, '', $data); // here comes your processing of the read data... } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fopen.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59885 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59885&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59885&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59885&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59885&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59885&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59885&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59885&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59885&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100406) next »