marco
marco

Reputation: 1752

execute php on web page button click

I have to execute a shell script when pressing a button on a web page. For this I'm using php so I have created a button in the file test.php

<form method="get" action="buildMaster.php">
    <input type="submit" value="Build Master" id="btnMaster">
</form>

when pressing the button, the php buildMaster.php is called:

<?php
    shell_exec('touch /Users/testUser/xxx');
?>

To test, I just touch a file to see if the script is called but nothing happens. The browser (Safari on Mac Lion) goes to buildMaster.php but nothing happens. What am I doing wrong ?

Upvotes: 0

Views: 147

Answers (2)

marco
marco

Reputation: 1752

Ok, found out. I have to redirect stderr to stdout.

<?php
    $output = shell_exec('touch /Users/testUser/xxx 2>&1');
    echo "<pre>$output</pre>";
?>

The output: touch: .... Permission denied

Upvotes: 0

Johny
Johny

Reputation: 1441

Try this:

<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>

And you'll see the output of your command

Also look to documentation: https://www.php.net/shell_exec - this function might be disabled if PHP is running in save mode.

Upvotes: 1

Related Questions