1

I want to run a php script on a button click in html. I've looked at a lot of the posts here, but they don't seem to work. As of now, I'm trying the solution here, but it doesn't work. I have no idea what's causing the trouble, but I know that it isn't the php file. Any help is appreciated.

php script:

<?php
shell_exec("/var/www/html/Camera/CameraScript.sh");
header('Location: /Camera/Camera.html?success=true');
?>

html code:

<form action="Script.php">
        <input type="submit" value="Open Script">
</form>

Shell Script:

#!/bin/bash

raspistill --output /var/www/html/Camera/Photos/Image.jpg
Community
  • 1
  • 1
Erik
  • 155
  • 1
  • 10
  • 2
    where is your code and your tries. Please show us your code and where you are facing issue. – RJParikh Nov 24 '16 at 04:47
  • does it redirect to the given url and have you checked apache error log for the shell_exec part? and do you have problem with permissions? when you run shell on terminal it's root user but in php it's apache user. what command do you try to run in your shell script? – Saeed.Gh Nov 24 '16 at 06:42
  • also shell_exec returns all of the output stream as a string instead of printing button is clicked trace something in script and try to print output of it – Saeed.Gh Nov 24 '16 at 06:50
  • @S.Gholizadeh I don't have problems with permissions and there isn't anything in the apache error log. I use ./CameraScript.sh – Erik Nov 25 '16 at 03:57
  • try to trace something in .sh file and print it out in php script. also it's better to provide .sh script here – Saeed.Gh Nov 25 '16 at 06:22
  • do you have permissions to execute this .sh file? note that php is mostly run as httpd user If you don't have permissions to execute this shell as apache user, you wouldn't get error in the error log, because it is not apache error. The easiest way to check is to put 755 permissions on your .sh file – bksi Nov 26 '16 at 03:17
  • @bksi I've put 755 permissions on it, but it still doesn't work. – Erik Nov 26 '16 at 03:34
  • Then your apache user couldn't have permissions to use raspistill ;) Basicly you want to take camera picture with form submit. Note that apache is limited user, and can have some restrictions on local resources – bksi Nov 26 '16 at 05:10
  • @bksi Sorry, I'm new to this, how would I fix that? – Erik Nov 26 '16 at 05:11
  • Actually you can't. This is security dillema :) If you add apache to root users you could solve this problem, but you would open a big hole in your system, because hackers can use this apache for different purpoces – bksi Nov 26 '16 at 05:13
  • also you can check raspistill permissions. If they are 755, you couldn't do much to solve this issue without security hole – bksi Nov 26 '16 at 05:14
  • Sorry bud, i don't know of other approaches for this dilema – bksi Nov 26 '16 at 05:16
  • Try searching "linux raspistill share camera with limited user" see if there are solutions for your problem – bksi Nov 26 '16 at 05:19
  • @bksi Thanks a lot, I'll see if I can google it. Cheers! – Erik Nov 26 '16 at 05:23
  • I think you could use crontab as priviledged user to run this command which should check for a file with content that makes it to take a picture. Then your php would just change this file content. It will be not native realtime, but it could work – bksi Nov 26 '16 at 06:01

2 Answers2

0

Try this:

<form action="script.php">
    <input type="submit" value="Open Script" name="btn">
</form>

script.php

if(isset($_REQUEST['btn']))
{
    echo 'Button is clicked';
    // you can your shell script here like: 
    // shell_exec("/var/www/html/Camera/CameraScript.sh");
}

When button is clicked, then the above message is display

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • Well, no it does say that the button is clicked, but when I have it execute a shell script, it doesn't work. Sorry if I didn't make it clear that the php script is executing a shell script. – Erik Nov 24 '16 at 05:18
0

If you have use html form like this...

<form action="script.php" method="post">
    <input type="submit" value="Open Script" name="btn">
</form>

it's must require put script.php php file in same directory...

<?php
 echo "Button Click";
 shell_exec("/var/www/html/Camera/CameraScript.sh");
 header('Location: /Camera/Camera.html?success=true');
?>

After click on button. if display Button Click then your php file is call and problems is in your php script...

Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34