note 59885 deleted from function.fopen by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Jun 2009 23:38:49 +0000 |
| Subject: | note 59885 deleted from function.fopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156560@lists.php.net to get a copy of this message | ||
Note Submitter: 151408626 dot unique at servux dot org
----
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...
}
?>