<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Initialize an inotify instance</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.inotify-add-watch.html">inotify_add_watch</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.inotify-queue-len.html">inotify_queue_len</a></div> <div class="up"><a href="ref.inotify.html">Inotify Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.inotify-init" class="refentry"> <div class="refnamediv"> <h1 class="refname">inotify_init</h1> <p class="verinfo">(PECL inotify >= 0.1.2)</p><p class="refpurpose"><span class="refname">inotify_init</span> — <span class="dc-title">Initialize an inotify instance</span></p> </div> <div class="refsect1 description" id="refsect1-function.inotify-init-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>inotify_init</strong></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment"> Initialize an inotify instance for use with <span class="function"><a href="function.inotify-add-watch.html" class="function">inotify_add_watch()</a></span> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.inotify-init-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A stream resource or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 examples" id="refsect1-function.inotify-init-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="inotify-init.example.basic"> <p><strong>Example #1 Example usage of inotify</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Open an inotify instance<br /></span><span style="color: #0000BB">$fd </span><span style="color: #007700">= </span><span style="color: #0000BB">inotify_init</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Watch __FILE__ for metadata changes (e.g. mtime)<br /></span><span style="color: #0000BB">$watch_descriptor </span><span style="color: #007700">= </span><span style="color: #0000BB">inotify_add_watch</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">, </span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">, </span><span style="color: #0000BB">IN_ATTRIB</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// generate an event<br /></span><span style="color: #0000BB">touch</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Read events<br /></span><span style="color: #0000BB">$events </span><span style="color: #007700">= </span><span style="color: #0000BB">inotify_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$events</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// The following methods allows to use inotify functions without blocking on inotify_read():<br /><br />// - Using stream_select() on $fd:<br /></span><span style="color: #0000BB">$read </span><span style="color: #007700">= array(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$write </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$except </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">stream_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$read</span><span style="color: #007700">,</span><span style="color: #0000BB">$write</span><span style="color: #007700">,</span><span style="color: #0000BB">$except</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// - Using stream_set_blocking() on $fd<br /></span><span style="color: #0000BB">stream_set_blocking</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">inotify_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">); </span><span style="color: #FF8000">// Does no block, and return false if no events are pending<br /><br />// - Using inotify_queue_len() to check if event queue is not empty<br /></span><span style="color: #0000BB">$queue_len </span><span style="color: #007700">= </span><span style="color: #0000BB">inotify_queue_len</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">); </span><span style="color: #FF8000">// If > 0, inotify_read() will not block<br /><br />// Stop watching __FILE__ for metadata changes<br /></span><span style="color: #0000BB">inotify_rm_watch</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">, </span><span style="color: #0000BB">$watch_descriptor</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Close the inotify instance<br />// This may have closed all watches if this was not already done<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array( array( 'wd' => 1, // Equals $watch_descriptor 'mask' => 4, // IN_ATTRIB bit is set 'cookie' => 0, // unique id to connect related events (e.g. // IN_MOVE_FROM and IN_MOVE_TO events) 'name' => '', // the name of a file (e.g. if we monitored changes // in a directory) ), ); </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.inotify-init-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.inotify-add-watch.html" class="function" rel="rdfs-seeAlso">inotify_add_watch()</a> - Add a watch to an initialized inotify instance</span></li> <li class="member"> <span class="function"><a href="function.inotify-rm-watch.html" class="function" rel="rdfs-seeAlso">inotify_rm_watch()</a> - Remove an existing watch from an inotify instance</span></li> <li class="member"> <span class="function"><a href="function.inotify-queue-len.html" class="function" rel="rdfs-seeAlso">inotify_queue_len()</a> - Return a number upper than zero if there are pending events</span></li> <li class="member"> <span class="function"><a href="function.inotify-read.html" class="function" rel="rdfs-seeAlso">inotify_read()</a> - Read events from an inotify instance</span></li> <li class="member"> <span class="function"><a href="function.fclose.html" class="function" rel="rdfs-seeAlso">fclose()</a> - Closes an open file pointer</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.inotify-add-watch.html">inotify_add_watch</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.inotify-queue-len.html">inotify_queue_len</a></div> <div class="up"><a href="ref.inotify.html">Inotify Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>