PHP/File Directory/popen
Содержание
- 1 Opening a Process File Pointer with popen()
- 2 Opening a Unidirectional Pipe Using popen()
- 3 Reading output from popen()
- 4 Reading standard error
- 5 Redirecting standard output
- 6 Using popen() and pclose()
- 7 Using popen() to Pass Data to the column Application
- 8 Using popen() to Read the Output of the Unix who Command
- 9 Administrators currently logged on to the server
Opening a Process File Pointer with popen()
<source lang="html4strict">
Its syntax is: int popen (string command, string mode) command refers to the system command that will be executed, mode refers to how you would use the popen() function to search a file: <?
$fh = fopen("data.txt","w"); fputs($fh, "A\n"); fputs($fh,"B\n"); fputs($fh,"C\n"); fclose($fh); $fh =popen("grep B < data.txt", "r"); fpassthru($fh);
?>
</source>
Opening a Unidirectional Pipe Using popen()
<source lang="html4strict">
<?php
$pr1 = popen("ls", "r"); echo fread($pr1, 1024); $pr2 = popen("php", "w"); fputs($pr2, "<?php touch("myfile.txt"); ?>");
?>
</source>
Reading output from popen()
<source lang="html4strict">
<?php $ph = popen("/sbin/route","r") or die($php_errormsg); while (! feof($ph)) {
$s = fgets($ph) or die($php_errormsg);
} pclose($ph) or die($php_errormsg); ?>
</source>
Reading standard error
<source lang="html4strict">
<?php $ph = popen("strace ls 2>&1","r") or die($php_errormsg); while (!feof($ph)) {
$s = fgets($ph) or die($php_errormsg);
} pclose($ph) or die($php_errormsg); ?>
</source>
Redirecting standard output
<source lang="html4strict">
<?php // Unix: just read standard error $ph = popen("strace ls 2>&1 1>/dev/null","r") or die($php_errormsg); // Windows: just read standard error $ph = popen("ipxroute.exe 2>&1 1>NUL","r") or die($php_errormsg); ?>
</source>
Using popen() and pclose()
<source lang="html4strict">
<?php
$command = "/usr/bin/dialog --inputbox "What is your Name" 0 0 2>/tmp/php_temp"; $pr = popen($command, "w"); $exit_code = pclose($pr); switch($exit_code) { case 0: $input = implode("", file("/tmp/php_temp")); echo "\nYou typed: $input\n"; break; case 1: echo "\nWhy did you cancel?\n"; break; }
?>
</source>
Using popen() to Pass Data to the column Application
<source lang="html4strict">
<html> <head> <title>Using popen() to Pass Data to the "column" Command</title> </head> <body>
<?php $products = array(
array( "A", 2, "red" ), array( "B", 3, "blue" ), array( "C", 1, "pink" ), array( "D", 1, "orange" ) );
$ph = popen( "column -tc 3 -s / > purchases/user3.txt", "w" )
or die( "Couldn"t open connection to "column" command" );
foreach ( $products as $prod ) {
fputs( $ph, join("/";, $prod). "\n");
} pclose( $ph ); ?>
</body> </html>
</source>
Using popen() to Read the Output of the Unix who Command
<source lang="html4strict">
<html> <head> <title>Using popen() to Read the Output of the Unix "who" Command</title> </head> <body>
Administrators currently logged on to the server
<?php $ph = popen( "who", "r" )or die( "Couldn"t open connection to "who" command" ); $host="demo.ru"; while ( ! feof( $ph ) ) {
$line = fgets( $ph, 1024 ); if ( strlen( $line ) <= 1 ) { continue; } $line = preg_replace( "/^(\S+).*/", "<a href=\"mailto:$1@$host\">$1</a>
\n", $line ); print "$line";
} pclose( $ph ); ?>
</body> </html>
</source>
Using popen() with nsupdate
<source lang="html4strict">
<?php $ph = popen("/usr/bin/nsupdate -k keyfile") or die($php_errormsg); if (-1 == fputs($ph,"update delete test.example.ru A\n")) { die($php_errormsg); } if (-1 == fputs($ph,"update add test.example.ru 5 A 192.168.1.1\n"))
{ die($php_errormsg); }
pclose($ph) or die($php_errormsg); ?.
</source>