簡體   English   中英

PHP exec不會執行Shell命令

[英]PHP exec won't execute shell commands

我的代碼:

<?php
include 'theme.php';
/*ceklogin();*/
css();
if($_POST['wget-send'])
    {
        $dir=$_POST['dir'];
        $link=$_POST['link'];
    exec('echo "'.$link.'" > /tmp/wget-download-link.txt',$out);
        exec('wget -P '.$dir.' -b -i /tmp/wget-download-link.txt -o /www/wget.log -c -t 100 -w 10',$out);
        echo $out[2];
        exit();
    }
echo "<br><br><form action=\"".$PHP_SELF."\" method=\"post\">";
echo "Download directory :<br><input type=\"text\" name=\"dir\" size=\"15\" value=\"/mnt/usb/\"/><br>";
echo '<br>Download link :<br>';
echo ("<textarea name=\"link\" rows=\"11\" cols=\"60\"></textarea><br><br>");
echo '<input type="submit" name="wget-send" value="Send" />';
echo "</form></div>";

foot();
echo '
</div>
</body>
</div>
</html>';
?>

它可以正常工作,wget將下載給定的URL,但是在我單擊“提交”按鈕后,它什么也沒有顯示,只有空白頁,但是當我更改此部分時:

echo "<br><br><form action=\"".$PHP_SELF."\" method=\"post\">";

像這樣

echo "<br><br><form action=wget_log.php method=\"post\">";

或任何php / html文件,wget不會執行任何操作,但會將我重定向到下一頁,我想做的是讓該wget運行並在單擊“提交”按鈕后轉到wget_log.php ,這是我的wget_log.php文件:

<?php
header("refresh: 5;");
include 'theme.php';
ceklogin();
css();
echo " Wget log :<br>";
echo "<textarea name=\"text-info\" rows=\"30\" cols=\"90\" readonly style=\"font-family: Arial;font-size: 7pt;\" >";
$datalines = file ("wget.log");
foreach ($datalines as $zz) {
echo $zz; }
echo "</textarea></div>";
foot();
echo '

</div>
</body>
</div>
</html>';
?>

@GolezTrol,我按照您的建議做了,並將字符串放在這里:

if($_POST['wget-send'])
    {
        $dir=$_POST['dir'];
        $link=$_POST['link'];
        exec('echo "'.$link.'" > /tmp/wget-download-link.txt',$out);
        exec('wget -P '.$dir.' -b -i /tmp/wget-download-link.txt -o /www/wget.log -c -t 100 -w 10',$out);
        echo $out[2];
        header('Location: wget_log.php');
        exit();
    }

echo "<br><br><form action=\"".$PHP_SELF."\" method=\"post\">";

但是我收到此錯誤Warning: Cannot modify header information - headers already sent by (output started at /www/wget.php:1) in /www/wget.php on line 12

更新2

<?php
header('Location: wget_log.php');
include 'theme.php';
/*ceklogin();*/
css();
if($_POST['wget-send'])
    {
        $dir=$_POST['dir'];
        $link=$_POST['link'];
    exec('echo "'.$link.'" > /tmp/wget-download-link.txt',$out);
        exec('wget -P '.$dir.' -b -i /tmp/wget-download-link.txt -o /www/wget.log -c -t 100 -w 10',$out);
        /*echo $out[2];*/
        exit();
    }
echo "<br><br><form action=\"".$PHP_SELF."\" method=\"post\">";
echo "Download directory :<br><input type=\"text\" name=\"dir\" size=\"15\" value=\"/mnt/usb/\"/><br>";
echo '<br>Download link :<br>';
echo ("<textarea name=\"link\" rows=\"11\" cols=\"60\"></textarea><br><br>");
echo '<input type="submit" name="wget-send" value="Send" />';
echo "</form></div>";

foot();
echo '
</div>
</body>
</div>
</html>';
?>

我以這種方式編輯了代碼,但仍然出現以下錯誤: Warning: Cannot modify header information - headers already sent by (output started at /www/wget.php:1) in /www/wget.php on line 2

無需獲取重定向頁面的內容並回顯它,您可以執行所需的任何操作,然后使用location標頭進行重定向:

header('Location: wget_log.php');

這就是所謂的Post / Redirect / Get模式

標頭應該是您在腳本中輸出的第一件事,當然,只有在保存文件時才必須執行標頭,否則,您將希望顯示表單而不是完全重定向。 因此,代碼的結構應如下所示。 為了簡潔起見,我省略了進行實際節省的代碼。 核心問題是:要使Location標頭(或任何標頭)正常工作,它必須是輸出的第一件事,因此在<?php開頭標記之前應該沒有HTML甚至空白,並且必須沒有其他回顯或在標題之前打印,或調用回顯某些內容的其他函數。

<?php

if($_POST['wget-send']) {
  // Save the File here..

  // Then redirect and terminate this script.
  header('Location: wget_log.php');
  exit();
}

// Normal page rendering in case of no file:
include 'theme.php';
/*ceklogin();*/
css();

// Form goes here.

暫無
暫無

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

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