一起看看PHP執行普通shell指令流程

coldplay.xixi
發布: 2023-04-09 14:22:01
轉載
4251 人瀏覽過

一起看看PHP執行普通shell指令流程

【相關學習推薦:php圖文教學

#這裡示範一些普通的shell指令

  php執行shell指令,可以使用下面幾個函數:

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

  注意的是:這三個函數在預設的情況下,都是被禁止了的,如果要使用這幾個函數,就要先修改php的設定檔php.ini,找出關鍵字disable_functions,將這一項中的這幾個函數名稱刪除掉,然後注意重新啟動apache。

  先看一下system()和passthru()兩個功能類似,可以互換:

<?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}成功执行"; } ?>
登入後複製

  執行結果如下:

#  

  注意,system()會將shell指令執行之後,立刻顯示結果,這一點會比較不方便,因為我們有時候不需要結果立刻輸出,甚至不需要輸出,所以可以用到exec()

#    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 "
"; ?>
登入後複製

  運行結果如下:

#php程式設計

########### (影片)#########

以上是一起看看PHP執行普通shell指令流程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:jb51.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!