1. 程式人生 > 實用技巧 >PHP-Filesystem Functions- 系統命令執行函式

PHP-Filesystem Functions- 系統命令執行函式

PHP中提供了幾個呼叫linux命令的函式,exec、system、passthru……

https://www.cnblogs.com/caozy/p/9261224.html

小結一下


01 system

執行外部程式,並且顯示輸出

 system ( string $command [, int &$return_var ] ) : string
  • 本函式就像是 C 語中的函式 system(),用來執行指令,並輸出結果。若是 return_var 引數存在,則執行 command 之後的狀態會填入 return_var 中
  • 如果 PHP 執行在伺服器模組中, system() 函式還會嘗試在每行輸出完畢之後, 自動重新整理 web 伺服器的輸出快取
  • 關於返回值:成功則輸出返回命令輸出的最後一行, 失敗則返回 FALSE。這些是和return_var中的返回狀態不同的部分

來自php.net的例子:

<?php
echo '<pre>';

// 輸出 shell 命令 "ls" 的返回結果
// 並且將輸出的最後一樣內容返回到 $last_line
// 將命令的返回值儲存到 $retval
$last_line = system('ls', $retval);

// 列印更多資訊
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: 
' . $retval; ?>

在windows系統中執行bat檔案刪除系統垃圾:

 <?php

    system(‘cmd.bat’,$callback);

    echo $callback;

  ?>//其中$callback變數為被執行檔案執行後的輸出資訊

在linux系統中實現tar檔案的線上解壓

  <?php

    system(‘tar xvf FileName.tar’,$callback);

    echo $callback;

    ?>

 

02 exec

執行一個外部程式

 exec ( string $command
[, array &$output [, int &$return_var ]] ) : string
  • 如果提供了 output 引數, 那麼會用命令執行的輸出填充此陣列, 每行輸出填充陣列中的一個元素。 陣列中的資料不包含行尾的空白字元,例如 \n 字元。 請注意,如果陣列中已經包含了部分元素,exec() 函式會在陣列末尾追加內容。如果你不想在陣列末尾進行追加, 請在傳入 exec() 函式之前 對陣列使用 unset() 函式進行重置
  • 如果同時提供 outputreturn_var 引數, 命令執行後的返回狀態會被寫入到此變數。
  • 關於返回值:命令執行結果的最後一行內容。 如果你需要獲取未經處理的全部輸出資料, 請使用 passthru() 函式;如果想要獲取命令的輸出內容, 請確保使用 output 引數。

總結:exec ()函式與system()類似,也執行給定的命令,但不輸出結果,而是返回結果的最後一行。雖然它只返回命令結果的最後一行,但用第二個引數array 可以得到完整的結果,方法是把結果逐行追加到array的結尾處。

來自php.net的例子:

<?php
// 輸出執行中的 php/httpd 程序的建立者使用者名稱
// (在可以執行 "whoami" 命令的系統上)
echo exec('whoami');
?>


03 popen

開啟程序檔案指標

 popen ( string $command , string $mode ) : resource
  • 開啟一個指向程序的管道,該程序由派生給定的 command 命令執行而產生。
  • 返回一個和 fopen() 所返回的相同的檔案指標,只不過它是單向的(只能用於讀或寫)並且必須用 pclose() 來關閉。此指標可以用於 fgets()fgetss()fwrite()。 當模式為 'r',返回的檔案指標等於命令的 STDOUT,當模式為 'w',返回的檔案指標等於命令的 STDIN。
  • 如果出錯返回 FALSE
  • 它允許訪問 shell 返回的任何錯誤資訊
  • 模式mode:

    來自php.net的的例子:

    <?php
    error_reporting(E_ALL);//報告所有的PHP錯誤
    
    /* 加入重定向以得到標準錯誤輸出 stderr。 */
    $handle = popen('/path/to/executable 2>&1', 'r');
    echo "'$handle'; " . gettype($handle) . "\n";
    $read = fread($handle, 2096);//讀取檔案(可安全用於二進位制檔案)
    echo $read;
    pclose($handle);
    ?>

    2>&1 的意思就是將標準錯誤重定向到標準輸出。這裡標準輸出已經重定向到了/path/to/executable。那麼標準錯誤也會輸出到/path/to/executable

    可以把/path/to/executable可以看作"黑洞". 它等價於一個只寫檔案. 所有寫入它的內容都會永遠丟失. 而嘗試從它那兒讀取內容則什麼也讀不到.

https://www.cnblogs.com/zhenghongxin/p/7029173.html


04 eval

把字串作為PHP程式碼執行

 eval ( string $code ) : mixed
  • 需要被執行的字串code:傳入的必須是有效的PHP程式碼。所有的語句必須以分號結尾。比如'echo"Hi!"'會導致一個parseerror,而'echo"Hi!";'則會正常執行。return 語句會立即中止當前字串的執行。
  • 關於返回值:eval() 返回 NULL,它只是負責執行,除非在執行的程式碼中 return 了一個值,函式返回傳遞給 return 的值
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>
This is a $string with my $name in it.
This is a cup with my coffee in it.


05 assert

檢查一個斷言是否為 FALSE

編寫程式碼時,我們總是會做出一些假設,斷言就是用於在程式碼中捕捉這些假設,可以將斷言看作是異常處理的一種高階形式。

assert() 會檢查指定的 assertion 並在結果為 FALSE 時採取適當的行動(視assert_options而定)。

assert_options

  • 'ASSERT_ACTIVE=1' // Assert函式的開關
  • 'ASSERT_WARNING =1' // 當表示式為false時,是否要輸出警告性的錯誤提示,issue a PHP warning for each failed assertion
  • 'ASSERT_BAIL= 0' // 是否要中止執行;terminate execution on failed assertions
  • 'ASSERT_QUIET_EVAL= 0' // 是否關閉錯誤提示,在執行表示式時;disable error_reporting during assertion expression evaluation
  • 'ASSERT_CALLBACK= (NULL)' // 是否啟動回撥函式 user function to call on failed assertions
# PHP5
bool assert ( mixed $assertion [, string $description ] ) 

# PHP7
bool assert ( mixed $assertion [, Throwable $exception ] )
<?php
// 啟用斷言,並設定它為 quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);

//建立處理函式
function my_assert_handler($file, $line, $code, $desc = null)
{
    echo "Assertion failed at $file:$line: $code";
    if ($desc) {
        echo ": $desc";
    }
    echo "\n";
}

// 設定回撥函式
assert_options(ASSERT_CALLBACK, 'my_assert_handler');

// Make an assertion that should fail
assert('2 < 1');
assert('2 < 1', 'Two is less than one');
?>
Assertion failed at test.php:21: 2 < 1
Assertion failed at test.php:22: 2 < 1: Two is less than one

如果 assertion 是字串,它將會被 assert() 當做 PHP 程式碼來執行。跟eval()類似, 不過eval($assertion)只是執行符合php編碼規範的$code_str。


06 passthru

執行外部程式並且顯示原始輸出

 passthru ( string $command [, int &$return_var ] ) : void
  • exec() 函式類似, passthru() 函式 也是用來執行外部命令(command)的。 當所執行的 Unix 命令輸出二進位制資料, 並且需要直接傳送到瀏覽器的時候, 需要用此函式來替代 exec()system() 函式。 常用來執行諸如 pbmplus 之類的可以直接輸出影象流的命令。 通過設定 Content-type 為 image/gif, 然後呼叫 pbmplus 程式輸出 gif 檔案, 就可以從 PHP 指令碼中直接輸出影象到瀏覽器。
  • 沒有返回值

區別:
system()輸出並返回最後一行shell結果。
exec()不輸出結果,返回最後一行shell結果,所有結果可以儲存到一個返回的數組裡面。
passthru()只調用命令,把命令的執行結果原樣地直接輸出到標準輸出裝置上。
相同點:都可以獲得命令執行的狀態碼