Home > Backend Development > PHP Tutorial > Let's take a look at the process of executing ordinary shell commands in PHP

Let's take a look at the process of executing ordinary shell commands in PHP

coldplay.xixi
Release: 2023-04-09 14:22:01
forward
4442 people have browsed it

Let's take a look at the process of executing ordinary shell commands in PHP

【Related learning recommendations: php graphic tutorial

Here are some common shell commands demonstrated

PHP execution shell command, you can use the following functions:

string system ( string $command [, int &$return_var ] )
string exec ( string $command [, array &$output [, int &$return_var ]] )
void passthru ( string $command [, int &$return_var ] )

Note: these three functions are prohibited by default Yes, if you want to use these functions, you must first modify the php configuration file php.ini, search for the keyword disable_functions, delete the function names in this item, and then pay attention to restart apache.

First, let’s take a look at system() and passthru(), which have similar functions and can be interchanged:

<?php
  $shell = "ls -la";
  echo "<pre class="brush:php;toolbar:false">";
  system($shell, $status);
  echo "
"; //注意shell命令的执行结果和执行返回的状态值的对应关系 $shell = "$shell"; if( $status ){ echo "shell命令{$shell}执行失败"; } else { echo "shell命令{$shell}成功执行"; } ?>
Copy after login

The execution results are as follows:

Note that system() will display the results immediately after executing the shell command. This is more inconvenient because sometimes we don’t need the results to be output immediately, or even output, so we can use exec()

Examples of using exec():

<?php
  $shell = "ls -la";
  exec($shell, $result, $status);
  $shell = "<font color=&#39;red&#39;>$shell</font>";
  echo "<pre class="brush:php;toolbar:false">";
  if( $status ){
    echo "shell命令{$shell}执行失败";
  } else {
    echo "shell命令{$shell}成功执行, 结果如下<hr>";
    print_r( $result );
  }
  echo "
"; ?>
Copy after login

The running results are as follows:

Related learning recommendations: php programming (video)

The above is the detailed content of Let's take a look at the process of executing ordinary shell commands in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template