Home  >  Article  >  Backend Development  >  How to use php popen() function

How to use php popen() function

青灯夜游
青灯夜游Original
2021-07-08 18:05:533608browse

In PHP, the built-in function popen() can open the process file pointer, which is used to open the pipeline to the program specified by the user using the command parameter. The syntax format is "popen(command, mode)", and the parameter mode is specified. Connection mode, the value can be r (read only) or w (write only).

How to use php popen() function

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

popen() is a built-in function in PHP , can open the process file pointer, which is used to open a pipe leading to the program specified by the user using the command parameter.

The popen() function returns a file pointer that is the same as the file pointer returned by fopen(), but it is essentially one-way, that is, it can only be used for reading or writing. The popen() pointer can be used with fgets(), fgetss(), and fwrite(). The file pointer started by popen() function must be closed with pclose().

Syntax:

popen(command,mode)
Parameters Description
command Required. Specifies the order to be executed.
mode Required. Specifies the connection mode.

Possible values:

  • r: Read only.
  • w: Write only (open and clear existing files or create a new file)

command and mode are sent as parameters To the popen() function, a one-way file pointer is returned on success, and FALSE is returned on failure.

Example:

<?php
$file = popen("/bin/ls","r");
//some code to be executed
pclose($file);
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use php popen() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn