system() to execute external shell scripts (from a form)
Posted: Fri Nov 27, 2009 8:09 pm
So, I'm super-new to PHP.. about 3-4 days in, and coming from a fairly strong /bin/sh background.
While executing these external shellscripts may not be quite the proper way, I need to learn to crawl before I can walk, run, fly, etc..
Towards that end, I'm trying to make a fairly simple front-end for 3 scripts I wrote that manually(ish) handle some image stuff for me.
When I execute the scripts on the cli as the user that the apache process is running as, they work fine, and display some relatively normal output to STDOUT.
When I execute them via system() statements in php, they properly give me the return value of 0 (success), but don't actually display STDOUT.
I've been beating my head on these for about the past 4 days getting to this point, I figured it was time to ask for some help..
There's a couple of problems with this code at the moment:
1) I don't get STDOUT redirected to the browser, I want to see the output from the shell scripts. *UPDATE* - Getting STDOUT now, just not formatted properly, PHP isn't passing the output the same way it's coming in. (Line breaks are getting dropped from multi-line output).
2) The third option for "push" doesn't work at all, and the code exits with the final else action.
3) \n doesn't break with a newline like I expect it to. *UPDATE* <br> works, I but multi-line output from the shellscript doesn't.
This is where I'm at with my current version:
<?php
$pull = $_POST["pull"];
$resize = $_POST["resize"];
$push = $_POST["push"];
$input = $_POST['sku'];
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Serwe's Image-o-matic</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
pull:<input type="checkbox" value="pull" name="pull"><br />
resize:<input type="checkbox" value="resize" name="resize"><br />
push:<input type="checkbox" value="push" name="push[]"><br />
<input type="submit" value="submit" name="submit">
</form>
<?php
} elseif ($pull == 'pull') {
$cmd='/bin/sh /usr/local/bin/pullimage.sh 2>&1';
$output = passthru($cmd, $retval);
// echo "System Output: $output\nReturn Value: $retval\n";
} elseif ($resize == 'resize') {
$cmd='/bin/sh /usr/local/bin/resizer.sh 2>&1';
$output = passthru($cmd, $retval);
// echo "System Output: $output\nReturn Value: $retval\n";
} elseif ($push == 'push') {
$cmd='/bin/sh /usr/local/bin/pushimages.sh 2>&1';
$output = passthru($cmd, $retval);
// echo "System Output: $output\nReturn Value: $retval\n";
} else {
echo "No button value found!";
}
?>
All help/comments appreciated, TIA.
Peter
While executing these external shellscripts may not be quite the proper way, I need to learn to crawl before I can walk, run, fly, etc..
Towards that end, I'm trying to make a fairly simple front-end for 3 scripts I wrote that manually(ish) handle some image stuff for me.
When I execute the scripts on the cli as the user that the apache process is running as, they work fine, and display some relatively normal output to STDOUT.
When I execute them via system() statements in php, they properly give me the return value of 0 (success), but don't actually display STDOUT.
I've been beating my head on these for about the past 4 days getting to this point, I figured it was time to ask for some help..
There's a couple of problems with this code at the moment:
1) I don't get STDOUT redirected to the browser, I want to see the output from the shell scripts. *UPDATE* - Getting STDOUT now, just not formatted properly, PHP isn't passing the output the same way it's coming in. (Line breaks are getting dropped from multi-line output).
2) The third option for "push" doesn't work at all, and the code exits with the final else action.
3) \n doesn't break with a newline like I expect it to. *UPDATE* <br> works, I but multi-line output from the shellscript doesn't.
This is where I'm at with my current version:
<?php
$pull = $_POST["pull"];
$resize = $_POST["resize"];
$push = $_POST["push"];
$input = $_POST['sku'];
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Serwe's Image-o-matic</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
pull:<input type="checkbox" value="pull" name="pull"><br />
resize:<input type="checkbox" value="resize" name="resize"><br />
push:<input type="checkbox" value="push" name="push[]"><br />
<input type="submit" value="submit" name="submit">
</form>
<?php
} elseif ($pull == 'pull') {
$cmd='/bin/sh /usr/local/bin/pullimage.sh 2>&1';
$output = passthru($cmd, $retval);
// echo "System Output: $output\nReturn Value: $retval\n";
} elseif ($resize == 'resize') {
$cmd='/bin/sh /usr/local/bin/resizer.sh 2>&1';
$output = passthru($cmd, $retval);
// echo "System Output: $output\nReturn Value: $retval\n";
} elseif ($push == 'push') {
$cmd='/bin/sh /usr/local/bin/pushimages.sh 2>&1';
$output = passthru($cmd, $retval);
// echo "System Output: $output\nReturn Value: $retval\n";
} else {
echo "No button value found!";
}
?>
All help/comments appreciated, TIA.
Peter