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 Using popen() with nsupdate
Opening a Process File Pointer with popen()
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);
?>
Opening a Unidirectional Pipe Using popen()
<?php
$pr1 = popen("ls", "r");
echo fread($pr1, 1024);
$pr2 = popen("php", "w");
fputs($pr2, "<?php touch("myfile.txt"); ?>");
?>
Reading output from popen()
<?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);
?>
Reading standard error
<?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);
?>
Redirecting standard output
<?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);
?>
Using popen() and pclose()
<?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;
}
?>
Using popen() to Pass Data to the column Application
<html>
<head>
<title>Using popen() to Pass Data to the "column" Command</title>
</head>
<body>
<div>
<?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 );
?>
</div>
</body>
</html>
Using popen() to Read the Output of the Unix who Command
<html>
<head>
<title>Using popen() to Read the Output of the Unix "who" Command</title>
</head>
<body>
<div>
<h1>Administrators currently logged on to the server</h1>
<?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><br />\n",
$line );
print "$line";
}
pclose( $ph );
?>
</div>
</body>
</html>
Using popen() with nsupdate
<?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);
?.