PHP Executing Program With Variables - php

I am trying to run a script that saves a derived number using PHP. I've confirmed that this script works manually from the debian terminal when I execute this command, it saves a file with my number and echos
"file saved"
./number-code-gen "12345678-1234-123456"
I've confirmed that the script works to an extent by running it without parameters using this from PHP:
$output = exec("./number-code-gen", $output2);
print_r($output2);
It echos back:
"No Arguments. Please enter a number."
I've tried all of the following with and without quotes around the argument (works either way in terminal)
exec('./nfz_core "12345678-12345678-12345678"', $output2);
print_r($output2);
$output = shell_exec('./nfz_core "12345678-12345678-12345678"');
echo $output;
passthru('./number-code-gen 12345678-1234-123456',$rtn);
echo $rtn;
I don't get echos or errors for either of these, and my file isn't generated. Is there a specific thing I'm missing here?
Basically, I just want to run ./number-code-gen 12345678-1234-123456 from PHP, but it isn't working. I am running Debian and PHP version: 5.6.33

Related

Better way to run python & display python errors from PHP

I am running a webserver for my laboratory that basically has a bunch of scripts I wrote in python for processing and analyzing tabular data.
I have a DigitalOcean droplet with a Laravel application deployed on it. When I want to run a script, I have the user upload some data file, and then from the PHP controller run:
shell_exec(python my_script.py arg1 arg2 etc);
The problem is, there are differences in dependencies and libraries between my development environment and 'production' environment. As such, when I try to run the script from the webserver and there is a python error, the object returned by shell_exec is just null. When the PHP blade template tries to parse/get data from this object, I get an error like so:
In this case, 'matchCount' is just a variable stored within a python list like this:
#Label peptides we found experimentally but do not have an in silico match for... as to predict contaminants
output = {
'sequence': protSeq,
'peptides': pepList,
'observablePeptideCount': str(len(pepList)),
'possibleObserved': possibleObserved,
'matchCount': matchCount,
'coverage': matchSumAA/protSeqAALength*100,
'massList': massList,
'tolerances': tolerances,
}
output = json.dumps(output)
The problem is, I understand the python script failed somewhere, but the error log does not give any indication of why or where. Is there some way I can have the webpage output the python error so I can correct it in the production environment?
Is there a better way to be doing all of this? Thank you for any help.
I won't recommend you print things out in production, but if it's a last resort you can try this:
<?php
$output = '';
$result = '';
exec('python my_script.py arg1 arg2 etc 2>&1', $output, $result);
var_dump($output); // all the output is here
var_dump($result); // gives an exit code, might be 1 if it's error

PHP website not running exec function when passing image as an argument

I'm having trouble running the "exec" function in my PHP website. I am able to run it several times with an executable that just takes in a variable argument and returns some test message. However, when I use an executable that does some image processing, where I want to pass an image from the website that a user uploads as an argument, it does not seem to be executing the executable at all. I even have some cout commands in the executable to ensure its running, but these are not being displayed on the website. So I think that for some reason the php cannot run the executable? I am able to run it fine from my desktop...
Here's an example of the code that isn't working on my PHP website:
$imgtest1="/uploaded_files/me.jpg";
$imgtest2="/uploaded_files/clusteroutput.jpg";
$nosuppix = 400;
$noweight = 100;
$executabletest = exec("ImgProc $imgtest1 $nosuppix $noweight $imgtest2");
echo $executabletest;
Is there a way to debug or get an error output from the exec function? Is there something I'm missing when passing an image to the executable? The executable uses several DLL files which are in the same folder as the executable. Do they need to be packaged together for some reason? I apologize, but I really don't know what's left to test...
***Edit: I'm now able to get it to run if I write out all the code in the escapeshellcmd itself... how come I'm not able to just pass the variables?
$cmdinput = escapeshellcmd('SuperpixelsFinal "D:/WebPages/TALIA ART/TALIA ART/uploaded_files/me.jpg" 400 100 "D:/WebPages/TALIA ART/TALIA ART/uploaded_files/clusteroutput.jpg"');
For anyone else having this issue, it is because I was using escapeshellcmd when I should have used escapeshellarg for each individual string and then run the exec with double quotes using only variables like so:
$executabletest = exec("SuperpixelsFinal $imgtest1 $nosuppix $noweight $imgtest2");

Trouble getting PHP to execute Lua script

So I'm working with PHP to attempt to execute a script I have in the same directory. The script runs fine locally and the permissions for the http-data user are set to be able to execute the script referenced in this block of PHP
$cmd = system('th neural_style.lua -style_image'.' ~/'.$style.'.jpg '.'-content_image '.$content_image.' -gpu 0 -backend cudnn -save_iter 1 -cudnn_autotune -output_image /var/www/html/processed/'.$email.'/out.png 2>&1', $retval);
echo '
</pre>
<hr />Recent output: ' . $last_line . '
<hr />Return value: ' . $retval;
The script should execute fine using the system method from what I understand, I know the variables look messy though this is the error I get from PHP:
sh: th: command not found
I set my default interpreter to bash instead of dash thinking that might be an issue, no dice. Torch is in the same directory, and like I said runs fine as my login.
I know what I'm trying to do in a way is like sacrilege, if there is a better way to run a script that takes 8 minutes roughly to complete using some user input from the web, I want to know. This is just what came natural to me. I'm looking to notify the user when the process is complete with an email anyways so any way of executing it is just dandy.
Edit: any mention of "http-data" was supposed to say "www-data".
Change the default shell for your http-data user to bash or dash. It is currently using sh.
Check what your $PATH variable is inside the PHP environment.

PHP shell_exec not producing desired output

I'm trying to run a java program and return the output then use that on my page.
if ($program) {
$output = shell_exec("java ftoa 12");
var_dump($output);
}
For some reason, that var_dump puts out null. I have ftoa.class in the same directory by the way. Also, the server my school is using runs an older version of php, not sure which one. Also when I run the command from the php command line, it works fine.
$:php -a
php>$output = shell_exec("java ftoa 12");
php>echo $output;
php>(desired output)

issuing things to shell and forgetting about it in php

how do you issue commands to the shell and then forget it's output and such? for example:
<?php
echo `sleep 2;echo hi`;
echo "foo";
?>
the result for this is hifoo. i would want a result that gives me foohi. why? i want the command issued to the shell simply issued and forgotten, i am confused about why PHP will wait for the result. is such a result possible?
(the idea behind this is setting up the correct number of selenium grid RC instances programatically. currently, it will stop after the first process is opened)
From php.net exec()
If a program is started with this
function, in order for it to continue
running in the background, the output
of the program must be redirected to a
file or another output stream. Failing
to do so will cause PHP to hang until
the execution of the program ends.
The same applies for all shell commands.
It's like anything you do at the command prompt, unless you take measures to put the new command in the background, the shell (and PHP ) will block until the command exits. Try this:
<?php
echo `(sleep 2; echo hi) &`;
echo 'foo';
?>
Note the brackets around your command, without that, the & woulud apply only to the echo , and you'd still have the 2 second pause.

Categories