I’m using the system() function in PHP to call a CLI program that I need to use that is not currently available from within PHP (code below). I’m using xml2brl (liblouisxml) CLI to generate an output file that contains braille ASCII text. Using the code below, the two files are successfully created, and the first file has the input text successfully written to it; however, the output file (the second file) never gets the translated text written to it.
Here’s the strange part, though. Using the same command that is passed to system() and the same temp files that I create in PHP, I can successfully run the command as my user in Terminal. What could this issue be caused by? When I run whoami command through system(), I get my user, and no special permissions are required for either the temp files or the CLI command xml2brl.
Currently, I’m creating two temp files in PHP:
//Create the temporary files that will be passed to xml2brl
$_standardText = tempnam("~/tmp", "pll_");
$_translatedText = tempnam("~/tmp", "pll_");
The temp files are stored in /private/tmp, and have the pll_ prefix to them, to identify the files that the PHP script is creating.
Then, I’m writing the contents of the passed text to the temp file like this:
//Write the contents of the passed text to the temp file
$handle = fopen($_standardText, "w");
fwrite($handle, $text);
fclose($handle);
Now, the temp file located in /private/tmp/xxxx has the text written in it (verified this), and then I go ahead and format the command:
$command = escapeshellcmd("xml2brl -p" . " " . $_standardText . " " . $_translatedText);
This has the format like this when echoing the $command variable:
xml2brl -p /private/tmp/pll_MYRy9m /private/tmp/pll_DmiK7E
And go ahead and run the exec command, which should process the input file and write the translated text to the output file, but it doesn’t:
exec($command);
Now, here’s the strange part… when I try to do just an ls, I can get the directory listing from my home directory and echo that out through the PHP script — signifying that I should have access to run the system and exec commands with Apache, but whenever this command runs, it doesn’t work. However, when I don’t delete the temp files, and use the same PHP-created command (xml2brl -p /private/tmp/pll_MYRy9m /private/tmp/pll_DmiK7E)and temp files in the CLI, I can run them under my user.
The first thing you should do (after making sure your script can run simple commands such as
/bin/ls) is make full use ofexec()‘s arguments:Second, make sure the paths are correct.
xml2brlmay be in your path, but perhaps not in the path of your script:If that still doesn’t work, you can redirect
stderr: