Home >Backend Development >PHP Tutorial >Application of pipe pipeline_PHP tutorial
Friends who use Linux must know the pipe function provided by the shell. Maybe you don’t know its full name, so have you ever used such a command:
cat INSTALL | more
This type of Command usage is developed using pipeline technology, which is different from redirection.
PHP provides the popen function to open a pipe:
int popen(string command, string mode);
popen() opens a pipe, which means opening the file pointer for processing. After opening a pipe, a file pointer is returned, and the subsequent usage is the same as reading and writing ordinary files. Take a look below:
$fp=popen("/bin/ls -l -FN /ect","r");
while(!feof($fp ))
ehco fgets($fp,4096)."
";
pclose($fp);
?>
You can try the output results.
Pipes are widely used. For example, we can open a sendmail pipe to send emails. Using pipes is simpler and easier to understand than using sockets. Because you must know how to interact with sendmail to use sockets, and there is no difference between preprocessing ordinary files in pipeline actions. You will understand if you look at the following program. This program will send an email to yqqfgq@china.com:
$fp=popen("/usr/sbin/sendmail yqqfgq@china. com","w");
$message="Hi! It's me, I'm yqqfgq!:)n";
fputs($fp,"Subject:$subjectn ");
fputs($fp,"From:yqqfgqn");
fputs($fp,"Reply-to:yqqfgq@china.com");
fputs($fp,$message) ;
fputs($fp,"
.
");
pclose($fp);
?>
It’s easy to use! Haha, that’s all. Brothers, please contact me if you have any comments. yqqfgq@ china.com