在php中調用linux命令的函數是
string exec(string command,string[array],int[return_var]);
- function exec_enabled() {
- $disabled = explode(', ', ini_get('disable_functions'));
- return !in_array('exec', $disabled);
- }
- <?php
- $tmp = exec("c:/Image/gm.exe convert c:/Image/file1.tiff c:/Image/file1.jpg", $results);
- ?>
還有一種命令是:php通過函數system()調用系統命令.
string system ( string $command [, int &$return_var ] )
實例:system('asterisk -vvvvvvvvvvvc');
system()是一樣的函數C的,它執行給定的命令和輸出結果的版本,該system()的調用也嘗試自動刷新網頁服務器的輸出緩沖器在每個輸出行如果PHP運行作為服務器模塊,如果你需要執行一個命令,并已全部通過直接從背面沒有任何干擾的命令數據,使用passthru()函數.
- $last_line = system('ls', $retval);
- function my_exec($cmd, $input='')
- {$proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes);
- fwrite($pipes[0], $input);fclose($pipes[0]);
- $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]);
- $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]);
- $rtn=proc_close($proc);
- return array('stdout'=>$stdout,
- 'stderr'=>$stderr,
- 'return'=>$rtn
- );
- } //開源代碼Vevb.com
- var_export(my_exec('echo -e $(</dev/stdin) | wc -l', 'h/nel/nlo'));
實例三,代碼如下:
- $cmd = "date";
- $output = system($cmd);
- printf("System Output: $output ");
- exec($cmd, $results);
- printf("Exec Output: {$results[0]} ");
php調用linux命令的權限問題
你可以使用定時任務執行你要調用的php,這時的權限就是root,php通過函數system()調用系統命令.
php一般是以apache用戶身份去執行的,把apache加入到存儲你文件的父文件夾屬組里去,然后改該父文件夾權限為775,這樣屬組成員就有寫的權限,而apache屬于這個組就可以改寫該目錄下所有文件的權限,當然,屬組最好不要是root,你可以為該文件夾改個其它普通用戶組.
改apache/php的運行用戶方法不安全.
新聞熱點
疑難解答