Heim  >  Artikel  >  Backend-Entwicklung  >  php执行linux系统命令的方法介绍

php执行linux系统命令的方法介绍

WBOY
WBOYOriginal
2016-07-25 08:52:05897Durchsuche
  1. $last_line = system("ls", $retval);
  2. echo "Last line of the output: " . $last_line;
  3. echo "
    Return value: " . $retval;
  4. ?>
复制代码

exec函数 说明:执行外部程序。 语法:string exec(string command, string [array], int [return_var]); 返回值: 字符串

详细介绍: 本函数执行输入 command 的外部程序或外部指令。它的返回字符串只是外部程序执行后返回的最后一行;若需要完整的返回字符串,可以使用 PassThru() 这个函数。

要是参数 array 存在,command 会将 array 加到参数中执行,若不欲 array 被处理,可以在执行 exec() 之前呼叫 unset()。若是 return_var 跟 array 二个参数都存在,则执行 command 之后的状态会填入 return_var 中。

值得注意的是若需要处理使用者输入的资料,而又要防止使用者耍花招破解系统,则可以使用 EscapeShellCmd()。

实例代码:

  1. echo exec("whoami");
  2. ?>
复制代码

popen函数 说明:打开文件。 语法:int popen(string command, string mode); 返回值: 整数

详细介绍: 本函数执行指令开档,而该文件是用管道方式处理的文件。用本函数打开的文件只能是单向的 (只能读或只能写),而且一定要用 pclose() 关闭。在文件操作上可使用 fgets()、fgetss() 与 fputs()。若是开档发生错误,返回 false 值。

实例代码:

  1. $fp = popen( "/bin/ls", "r" );
  2. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn