简体   繁体   中英

PHP exec won't execute shell commands

My code:

<?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>';
?>

It works, wget will download the urls given but after I click the submit button it shows nothing, only a blank page, but when I change this part:

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

to be like this

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

or any php/html files, wget won't do anything but it'll redirect me to the next page, what I'd like to do is have that wget run and go to wget_log.php after I click the submit button, here's my wget_log.php file:

<?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, I did what you suggested and put the string here:

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\">";

But I get this error Warning: Cannot modify header information - headers already sent by (output started at /www/wget.php:1) in /www/wget.php on line 12

Update 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>';
?>

I edited my code like that but still get the following error: Warning: Cannot modify header information - headers already sent by (output started at /www/wget.php:1) in /www/wget.php on line 2

Instead of fetching the content of the redirect page and echoing it, you can perform whatever you need to do, and then redirect using the location header:

header('Location: wget_log.php');

This is known as the Post/Redirect/Get pattern .

The header should be the first thing you output in your script, and of course you only must do it when you save the file, otherwise you will want to show the form and not redirect at all. So the structure of the code should look like this. I left out the code that does the actual saving for brevity. Core thing is: for the Location header (or any header) to work, it must be the first thing that is outputted, so there should be no HTML or even whitespace before the <?php opening tag, and there must be no other echoes or prints before the header either, or calls to other functions that echo something.

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM