
- PHP 教程
- PHP - 首頁
- PHP - 簡介
- PHP - 安裝
- PHP - 歷史
- PHP - 特性
- PHP - 語法
- PHP - Hello World
- PHP - 註釋
- PHP - 變數
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 變數
- PHP - 常量
- PHP - 魔術常量
- PHP - 資料型別
- PHP - 型別轉換
- PHP - 型別混淆
- PHP - 字串
- PHP - 布林值
- PHP - 整數
- PHP - 檔案與 I/O
- PHP - 數學函式
- PHP - Heredoc 和 Nowdoc
- PHP - 複合型別
- PHP - 檔案包含
- PHP - 日期與時間
- PHP - 標量型別宣告
- PHP - 返回型別宣告
- PHP 運算子
- PHP - 運算子
- PHP - 算術運算子
- PHP - 比較運算子
- PHP - 邏輯運算子
- PHP - 賦值運算子
- PHP - 字串運算子
- PHP - 陣列運算子
- PHP - 條件運算子
- PHP - 展開運算子
- PHP - 空值合併運算子
- PHP - 鍵值比較運算子
- PHP 控制語句
- PHP - 決策
- PHP - If…Else 語句
- PHP - Switch 語句
- PHP - 迴圈型別
- PHP - For 迴圈
- PHP - Foreach 迴圈
- PHP - While 迴圈
- PHP - Do…While 迴圈
- PHP - Break 語句
- PHP - Continue 語句
- PHP 函式
- PHP - 函式
- PHP - 函式引數
- PHP - 按值傳遞
- PHP - 按引用傳遞
- PHP - 預設引數
- PHP - 具名引數
- PHP - 可變引數
- PHP - 返回值
- PHP - 傳遞函式
- PHP - 遞迴函式
- PHP - 型別提示
- PHP - 變數作用域
- PHP - 嚴格型別
- PHP - 匿名函式
- PHP - 箭頭函式
- PHP - 可變函式
- PHP - 區域性變數
- PHP - 全域性變數
- PHP 超全域性變數
- PHP - 超全域性變數
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 檔案處理
- PHP - 檔案處理
- PHP - 開啟檔案
- PHP - 讀取檔案
- PHP - 寫入檔案
- PHP - 檔案是否存在
- PHP - 下載檔案
- PHP - 複製檔案
- PHP - 追加檔案
- PHP - 刪除檔案
- PHP - 處理 CSV 檔案
- PHP - 檔案許可權
- PHP - 建立目錄
- PHP - 列出檔案
- 面向物件 PHP
- PHP - 面向物件程式設計
- PHP - 類和物件
- PHP - 建構函式和解構函式
- PHP - 訪問修飾符
- PHP - 繼承
- PHP - 類常量
- PHP - 抽象類
- PHP - 介面
- PHP - 特性
- PHP - 靜態方法
- PHP - 靜態屬性
- PHP - 名稱空間
- PHP - 物件迭代
- PHP - 封裝
- PHP - Final 關鍵字
- PHP - 過載
- PHP - 克隆物件
- PHP - 匿名類
- PHP Web 開發
- PHP - Web 概念
- PHP - 表單處理
- PHP - 表單驗證
- PHP - 表單郵件/URL
- PHP - 完整表單
- PHP - 檔案包含
- PHP - GET 和 POST
- PHP - 檔案上傳
- PHP - Cookies
- PHP - Sessions
- PHP - Session 選項
- PHP - 傳送郵件
- PHP - 淨化輸入
- PHP - Post-Redirect-Get (PRG)
- PHP - 快閃記憶體訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 編碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - Bug 除錯
- PHP - 針對 C 開發人員
- PHP - 針對 PERL 開發人員
- PHP - 框架
- PHP - Core PHP 與框架
- PHP - 設計模式
- PHP - 過濾器
- PHP - JSON
- PHP - 異常
- PHP - 特殊型別
- PHP - 雜湊
- PHP - 加密
- PHP - is_null() 函式
- PHP – 系統呼叫
- PHP - HTTP 認證
- PHP - 交換變數
- PHP - Closure::call()
- PHP - 過濾後的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 預期
- PHP - Use 語句
- PHP - 整數除法
- PHP - 已棄用的功能
- PHP - 已刪除的擴充套件和 SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 程序
- PHP - PDO 擴充套件
- PHP - 內建函式
- PHP 有用資源
- PHP - 速查表
- PHP - 問答
- PHP - 快速指南
- PHP - 線上編譯器
- PHP - 有用資源
- PHP - 討論
PHP – 系統呼叫
PHP 的內建函式庫包含一類用於在 PHP 程式碼中呼叫作業系統實用程式和外部程式的函式。在本章中,我們將討論用於執行系統呼叫的 PHP 函式。
system() 函式
system() 函式類似於 C 中的 system() 函式,它執行給定的命令並輸出結果。
system(string $command, int &$result_code = null): string|false
如果 PHP 作為伺服器模組執行,則 system() 呼叫嘗試在每行輸出後自動重新整理 Web 伺服器的輸出緩衝區。成功時返回命令輸出的最後一行,失敗時返回 false。
示例
以下 PHP 程式碼片段呼叫 Windows 作業系統的 DIR 命令,並顯示當前目錄中的檔案列表。
<?php echo '<pre>'; // Outputs all the result of DOS command "dir", and returns // the last output line into $last_line. Stores the return value // of the shell command in $retval. $last_line = system('dir/w', $retval); // Printing additional info echo ' </pre> <hr />Last line of the output: ' . $last_line . ' <hr />Return value: ' . $retval; ?>
它將產生以下輸出 -
Volume in drive C has no label. Volume Serial Number is 7EE4-E492 Directory of C:\xampp\htdocs [.] [..] applications.html bitnami.css [dashboard] employee.csv favicon.ico hello.csv hello.html hello.php homepage.php [img] index.php [Langi] menu.php myform.php myname.php new.png new.txt test.php test.zip [TPcodes] uploadfile.php [webalizer] welcome.png [xampp] 18 File(s) 123,694 bytes 8 Dir(s) 168,514,232,320 bytes free Last line of the output: 8 Dir(s) 168,514,232,320 bytes free Return value: 0
shell_exec() 函式
shell_exec() 函式與 PHP 的反引號運算子相同。它透過 shell 執行給定的命令,並將完整的輸出作為字串返回。
shell_exec(string $command): string|false|null
該函式返回一個包含已執行命令輸出的字串,如果管道無法建立則返回 false,如果發生錯誤或命令沒有產生輸出則返回 null。
示例
在以下程式碼中,我們使用 shell_exec() 函式在當前目錄中獲取副檔名為“.php”的檔案列表 -
<?php $output = shell_exec('dir *.php'); echo "<pre>$output</pre>"; ?>
它將產生以下輸出 -
Volume in drive C has no label. Volume Serial Number is 7EE4-E492 Directory of C:\xampp\htdocs 10/26/2023 08:27 PM 73 hello.php 10/12/2023 10:40 AM 61 homepage.php 07/16/2015 09:02 PM 260 index.php 10/12/2023 10:39 AM 49 menu.php 09/25/2023 01:43 PM 338 myform.php 10/12/2023 10:49 AM 51 myname.php 10/26/2023 02:00 PM 369 test.php 09/25/2023 01:42 PM 555 uploadfile.php 8 File(s) 1,756 bytes 0 Dir(s) 168,517,771,264 bytes free
exec() 函式
exec() 函式將給定的命令作為字串引數執行。
exec(string $command, array &$output = null, int &$result_code = null):string|false
如果指定了$output 引數,則它是一個數組,其中將填充來自命令的每一行輸出。
示例
在這種情況下,我們使用 exec() 函式從程式內部呼叫 whoami 命令。whoami 命令返回使用者名稱。
<?php // outputs the username that owns the running php/httpd process // (on a system with the "whoami" executable in the path) $output=null; $retval=null; exec('whoami', $output, $retval); echo "Returned with status $retval and output:\n"; var_dump($output); ?>
它將產生以下輸出 -
Returned with status 0 and output: array(1) { [0]=> string(13) "gnvbgl3\mlath" }
passthru() 函式
passthru() 函式執行外部程式並顯示原始輸出。儘管 passthru() 函式類似於 exec() 或 system() 函式,因為它執行命令,但當來自 OS 命令的輸出是需要直接傳遞迴瀏覽器的二進位制資料時,應該使用它代替它們。
示例
一個使用 passthu() 函式顯示系統 PATH 環境變數內容的 PHP 程式
passthru(string $command, int &$result_code = null): ?false <?php passthru ('PATH'); ?>
它將產生以下輸出 -
PATH=C:\Python311\Scripts\;C:\Python311\;C:\WINDOWS\system32;C:\WINDOWS; C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\; C:\WINDOWS\System32\OpenSSH\;C:\xampp\php;C:\Users\mlath\AppData\Local \Microsoft\WindowsApps;C:\VSCode\Microsoft VS Code\bin
反引號運算子
PHP 支援一個執行運算子:反引號 (``)。(它們不是單引號!)PHP 將嘗試將反引號中的內容作為 shell 命令執行;將返回輸出。反引號運算子的使用與 shell_exec() 相同。
示例
看看下面的例子 -
<?php $output = `dir *.php`; echo "<pre>$output</pre>"; ?>
它將產生以下輸出 -
Volume in drive C has no label. Volume Serial Number is 7EE4-E492 Directory of C:\xampp\htdocs 10/26/2023 08:42 PM 61 hello.php 10/12/2023 10:40 AM 61 homepage.php 07/16/2015 09:02 PM 260 index.php 10/12/2023 10:39 AM 49 menu.php 09/25/2023 01:43 PM 338 myform.php 10/12/2023 10:49 AM 51 myname.php 10/26/2023 02:00 PM 369 test.php 09/25/2023 01:42 PM 555 uploadfile.php 8 File(s) 1,744 bytes 0 Dir(s) 168,471,289,856 bytes free
當shell_exec() 被停用時,反引號運算子也被停用。