簡體   English   中英

PHP-系統功能-/ usr / bin / echo您好:沒有這樣的文件或目錄

[英]Php - System function - /usr/bin/echo Hello : No such file or directory

我是PhP的新手。
當我在php中使用System功能運行linux終端命令時,出現error_log錯誤。
這是我的代碼:

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  

我有什么想念的嗎?
提前致謝。

僅嘗試使用echo,而不嘗試使用/ usr / bin / echo
更改命令變量聲明,如下所示:

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

您只想將$ name變量的值添加到路徑中,對嗎? 如果是這樣,只需將其寫為

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

php將解析雙qoutes之間的字符串中的變量,請參閱string.parsing

最后,我借助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;  
}  

謝謝大家的及時答復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM