system() to execute external shell scripts (from a form)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peterdawolf
Forum Newbie
Posts: 1
Joined: Fri Nov 27, 2009 8:02 pm

system() to execute external shell scripts (from a form)

Post by peterdawolf »

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
Last edited by peterdawolf on Fri Nov 27, 2009 8:43 pm, edited 2 times in total.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: system() to execute external shell scripts (from a form)

Post by daedalus__ »

im really the wrong person to ask about this. im on windows and haven't ever had a use for system(). this is a shot in the dark but if
Return Values

Returns the last line of the command output on success, and FALSE on failure.
is the last line of output blank? a lot of of dos commands output a blank line right before returning to the prompt

also \n returns a linefeed not a html line break (<br />)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: system() to execute external shell scripts (from a form)

Post by AbraCadaver »

Couple of things.

1. As stated, browsers don't display newlines. You must use the br tag. passthru() doesn't return the output, it flushes it to the browser. This might help out:

Code: Select all

exec($cmd, $output);
echo implode("<br />", $output);
2. Here you have defined push to be an array:

Code: Select all

push:<input type="checkbox" value="push" name="push[]">
If you would do a print_r($_POST); you would see. Change push[] to push. In addition, the else will always fire unless the first IF condition is true even if one of the ELSEIFS is true.

FWIW your logic is flawed. You are allowing the user to select all 3 checkboxes, but the IF statement only allows one action to be performed. If you only want one choice then use radio buttons or maybe a dropdown. If you want multiple choices then maybe have all of the checkboxes in an array and loop through them:

Code: Select all

pull:<input type="checkbox" value="pull" name="action[]"><br />
resize:<input type="checkbox" value="resize" name="action[]"><br />
push:<input type="checkbox" value="push" name="action[]"><br />
Let me know if you need help.

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply