Just as PHP offers the fopen() and fclose() functions to open and close file handles, there's also the popen() and pclose() functions, which can be used to open uni-directional handles to processes. Once a process handle has been created, data can be read from it or written to it using the standard fgets(), fputs(), fread() and fwrite() file functions. Consider the following example, which demonstrates by opening a pipe to the "cat" command:
As you can see, opening a pipe to a process and reading from it is very similar to opening and reading a file. As with files, the first step is to obtain a handle to the process with popen() - this handle serves as the foundation for all future communication. Once a handle has been obtained, data can be read from, or written to, the handle using the file input/output functions you're already familiar with. The handle can be closed at any time with the pclose() function. If you need bi-directional communication, PHP 4.3 also offers the new
blog comments powered by Disqus |