Reading from and writing to system command pipes
 
 
 

You can use the popen and pclose commands to read and write data through a pipe to a system command as if it were a file.

Like fopen, popen opens a pipe for reading or writing depending on the second mode argument ("r" or "w") and returns a file handle representing the pipe. You can then use the standard file functions for reading and writing on the pipe’s file handle (fprint, fgetword, fgetline, etc.).

If popen returns 0 something went wrong with the system command.

For example:

// Unix specific example. Note: if you really want to get a directory
// listing, please use the "getFileList" command instead. This is just
// used as a simple example.
//
$pipe = popen( "ls -1", "r" );
string $dirList[];
while ( !feof( $pipe ) ) {
 $dirList[size( $dirList )] = fgetline( $pipe );
}
pclose( $pipe );
// Windows specific example. Note: if you really want to get a directory
// listing, please use the "getFileList" command instead. This is just
// used as a simple example.
//
$pipe = popen( "DIR /B", "r" );
string $dirList[];
while ( !feof( $pipe ) ) {
 $dirList[size( $dirList )] = fgetline( $pipe );
}
pclose( $pipe );