PHP exec不会执行shell命令

时间:2014-12-23 09:01:16

标签: php html

我的代码:

<?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会下载给出的网址,但点击提交按钮后它没有显示任何内容,只显示空白页面,但是当我更改此部分时:

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

1 个答案:

答案 0 :(得分:2)

您可以执行任何需要执行的操作,然后使用位置标头重定向,而不是获取重定向页面的内容并回显它:

header('Location: wget_log.php');

这称为Post/Redirect/Get pattern

标题应该是您在脚本中输出的第一个内容,当然您只需在保存文件时执行此操作,否则您将需要显示表单而不是重定向。所以代码的结构应该是这样的。为简洁起见,我遗漏了实际保存的代码。核心的事情是:要使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.