PHP exec() using string to call program with command-line parameters

85 Views Asked by At

My web page calls a php file to execute gphoto2. Here's the PHP

<?php
$cmdexec='gphoto2 --auto-detect --set-config shutterspeed=' . $_POST["shutterspeed"] .' --set-config iso=' . $_POST["iso"] . ' --filename=/home/photos/' . $_POST["filename"] . ' --capture-image-and-download';
echo '<pre>The command is: '.$cmdexec.'<br>';
$photo=exec($cmdexec, $cmdrtn);
?> 

The echo returns:

The command is: gphoto2 --auto-detect --set-config shutterspeed=40 --set-config iso=5 --filename=/home/photos/20230310154522.jpg --capture-image-and-download

If I copy that command (from "gphoto2" to the end of the line) and paste it on the command line, it works perfectly (the camera is triggered and the file is downloaded.) The same is true if I execute it from the PHP interactive environment. But on the web page the result I get is:

Array ( [0] => Model Port [1] => ---------------------------------------------------------- [2] => Canon Digital Rebel XT (normal mode) usb:002,007 )

I can get that same result from the command-line by typing gphoto2 --auto-detect without all the other parameters. Clearly, gphoto2 is being executed, but not the way I would like. That suggests that the rest of the parameters either aren't being passed to gphoto2 or are being passed in way that gphoto2 ignores them.

Is this how I should be calling exec()? What am I doing wrong?

Note 1: This is an off-line, localhost web site, so command injection is not an issue. Note 2: Xubuntu 22.04, Apache2 2.4.52, PHP 8.1, gphoto2 2.5.27 Note 3: This code used to work on this machine. It broke after one of the upgrades. Note 4: I have also tried escaping the spaces in the string (for example: gphoto2 --auto-detect --set-config ... etc.) with no change.

1

There are 1 best solutions below

0
Duston On

The problem turned out to be a permissions issue with the USB device. I fixed it with:

chmod u+s /usr/bin/gphoto2