2014-11-04 36 views
0

我是PhP的新手。
當我在php中使用System函數運行linux終端命令時,我在error_log中收到錯誤。
這裏是我的代碼:Php - 系統函數 -/usr/bin/echo你好:沒有這樣的文件或目錄

if(isset($_POST['submit_button'])) 
{ 
    $name=$_POST['User_name']; // here $name contains 'John' 
    echo '<pre>'; 

    $command="/usr/bin/echo $name"; 
    $command1="'".$command."'"; 
    $last_line = system($command1, $retval); 

    echo '</pre> 
    <hr />Last line of the output: ' . $last_line . ' 
    <hr />Return value: ' . $retval;   
} 

當我運行這段代碼,我發現了以下錯誤:

in browser - giving code : 127 


in /var/log/httpd/error_log file - sh: /usr/bin/echo John: No such file or directory 

我錯過了什麼?
在此先感謝。

回答

0

最後,我解決了這樣的錯誤與escapeshellarg()
下面是更新後的代碼的幫助..

if(isset($_POST['submit_button'])) 
{ 
    $name=$_POST['User_name']; // here $name contains 'John' 
    echo '<pre>'; 

    $last_line = system('/usr/bin/echo'.escapeshellarg($name)); 
             // here escapeshellarg() does the trick 

    echo '</pre> 
    <hr />Last line of the output: ' . $last_line . ' 
    <hr />Return value: ' . $retval; 
} 

謝謝大家的直接回復。

0

嘗試用只能隨聲附和,而不是在/ usr /斌/回聲
更改命令變量聲明如下:

$command="echo $name"; 
+0

獲取此錯誤 - ** sh:echo約翰:命令未找到** – 2014-11-04 07:33:01

+0

因爲您正在運行$ commmand1,它被單引號括起來,這些引號被視爲字符串而不是命令。刪除行$ command1 =「'」。$ command。「'」;並嘗試使用$ last_line = system($ command,$ retval); – 2014-11-07 03:40:24

0
$command="/usr/bin/echo $name"; 

你只是想添加$ name變量的值到正確的道路?如果是這樣只是寫爲

$command="/usr/bin/$name"; 

PHP將解析雙qoutes之間的字符串變量,看string.parsing

相關問題