繁体   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