Home > Backend Development > PHP Tutorial > Introduction to the method of executing linux system commands in php

Introduction to the method of executing linux system commands in php

WBOY
Release: 2016-07-25 08:52:05
Original
944 people have browsed it
  1. < ?php
  2. $last_line = system("ls", $retval);
  3. echo "Last line of the output: " . $last_line;
  4. echo "
    Return value: " . $retval;
  5. ?>
Copy code

exec function Description: Execute external program. Syntax: string exec(string command, string [array], int [return_var]); Return value: string

Detailed introduction: This function executes the external program or external instruction input command. Its return string is only the last line returned after the external program is executed; if you need a complete return string, you can use the PassThru() function.

If the parameter array exists, the command will add the array to the parameter and execute it. If you do not want the array to be processed, you can call unset() before executing exec(). If both return_var and array parameters exist, the status after executing the command will be filled in return_var.

It is worth noting that if you need to process the data entered by the user and prevent the user from playing tricks to crack the system, you can use EscapeShellCmd().

Example code:

  1. < ?php
  2. echo exec("whoami");
  3. ?>
Copy code

popen function Description: Open the file. Syntax: int popen(string command, string mode); Return value: integer

Detailed introduction: This function executes the command to open a file, and the file is processed by pipeline. Files opened with this function can only be one-way (read only or write only), and must be closed with pclose(). For file operations, fgets(), fgetss(), and fputs() can be used. If an error occurs while opening the file, a false value is returned.

Example code:

  1. < ?
  2. $fp = popen( "/bin/ls", "r" );
  3. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template