Execution of a Python Script from PHP - php

I am making an android application in which I am first uploading the image to the server and on the server side, I want to execute a Python script from PHP. But I am not getting any output. When I access the Python script from the command prompt and run python TestCode.py it runs successfully and gives the desired output. I'm running Python script from PHP using the following command:
$result = exec('/usr/bin/python /var/www/html/Source/TestCode.py');
echo $result
However, if I run a simple Python program from PHP it works.
PHP has the permissions to access and execute the file.
Is there something which I am missing here?

exec('/usr/bin/python /var/www/html/Source/TestCode.py', $output);
var_dump($output);
2nd Parameter of exec will give output
EDIT:
exec('/usr/bin/python /var/www/html/Source/TestCode.py 2>&1', $output);
2>&1 - redirecting stderr to stdout. Now in case of any error too, $output will be populated.

First Check your python PATH using "which python" command and check result is /usr/bin/python.
Check your "TestCode.py" if you have written #!/usr/bin/sh than replace it with #!/usr/bin/bash.
Than run these commands
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $result);
echo $result

Related

How to run a Node.js script from PHP with XAMPP?

I am using XAMPP on Windows 7 to run a localhost, and I'm trying to use the PHP exec function to execute a Node.js script.
I am able to successfully use the exec function to run PhantomJS scripts, but when I try to do the same thing for Node.js, it doesn't work.
He's an example of a PHP script that properly runs a PhantomJS script:
<?php
exec('/phantomjs/bin/phantomjs /phantomjs/scripts/test.js', $output);
print_r($output);
And here's a similar example of a Node.js script that outputs an empty array every time:
<?php
exec('/Program Files (x86)/nodejs/node /Program Files (x86)/nodejs/test.js', $output);
print_r($output);
I'm sure that all of my paths are correct and that the Node.js script executes correctly whenever I execute it directly from the command line, but I can't get anything to return from the Node.js script when I run it from the PHP exec command. I also tried the following script, but I still get nothing:
<?php
exec('/Program Files (x86)/nodejs/node -v', $output);
print_r($output);
Any advice on what I'm missing would be greatly appreciated. Thank you.

How to run exe file in php

I'm working on classification algorithm in python and it related with php code, to get the right result I should run my python exe then go to the php code.
I want to execute the python by itself with php code. I tried this:
<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');
?>
But It's not working, should I know more about path? and how to put my exe in the write path?
<?php
exec("C:\\Users\\posh\\AppData\\Local\\Programs\\Python\\Python35\\python.exe YOUR_PYTHONSCRIPT.py <arguments if any>", $output, $ret_code);
// directly writing python may not work in that case
// give path to python.exe
exec("python YOUR_PYTHONSCRIPT.py <arguments if any>", $output, $ret_code);
// $ret_code : returns the code 0 or 1
// output is an array
?>

Why my PHP doesn't run the same command as specified in cmd?

I'm using a windows machine.
When I run this command in cmd it works fine.
C:\wamp\www\upload\cprogram.exe > output.txt
But when I write the same command in my php it shows
"> not recognised as an internal or external command"
My php code :
$exepath="C:\wamp\www\upload\cprogram.exe";
$outputpath="C:\wamp\www\upload\output.txt";
exec("$exepath > $outputpath");
Please tell me how can i send my executed C program output a file?
Please tell me how can i send my executed C program output a file?
There are many ways that you can write the output of your command to a file using php.
In your example it doesnt work due to the use of concatenation. This should work better:
exec($exepath.' > '.$outputpath);
Another option would be to use the shell_exec command instead:
$exepath="C:\wamp\www\upload\cprogram.exe";
$outputpath="C:\wamp\www\upload\output.txt";
shell_exec ($exepath ." > ". $outputpath);
Or just use the system command and write the output to a file yourself:
$exepath="C:\wamp\www\upload\cprogram.exe";
$outputpath="C:\wamp\www\upload\output.txt";
system($exepath, $return);
$fp = fopen($outputpath, 'w');
fwrite($fp, $return);
fclose($fp);

Executing Perl through PHP returns Exit Status 2

I am attempting to execute a Perl script through PHP via the following command:
$last_line = exec('/usr/bin/perl /path/to/perl/script.pl ' . escapeshellarg($argument),$output,$status);
The script does not perform its function, and the exit status is always 2 (improper use of shell builtins). Both Perl and the script can be read and executed by any user. Running the script on a command line works just fine. Any thoughts?
I use this code to run file.pl located in same folder as php file
$output = passthru("perl relative_path/file.pl ".$_POST["var1"]." ".$_POST["var2"]);
I use a perl script as a database interface from PHP with the following code:
exec ( $cmdToPass . " 2>&1" , $output , $returnVar);
Where:
$cmdToPass is the command I'm executing (the perl script) with stderr redirected to stdout.
$output holds the command's output
$returnVar holds the exit code
You should then be able to print $output to determine why the script. You may also wish to check the PHP error log (usually the same as the web server's error log) to see if anything is captured there.
See the PHP manual on exec if you need more info on the exec function.

PHP - shell_exec output in browser is empty

I'm running a simple wget command in shell_exec()
wget_file.php
<?php
$command = "wget http://mydomain.co/media/bigbigbig.wav";
$output = shell_exec($command);
echo $output;
?>
According to http://www.php.net/shell_exec I can possibly expect an output:
"The output from the executed command or NULL if an error occurred or the command produces no output."
If I run wget_file.php from the command line, I'll see a display of the wget results.
However, if I run it from a browser, no result is given. (but the file does indeed download successfully)
I plan to execute the wget_file.php by calling via cUrl, while passing the url + path.
But would be nice to get a response from the shell_exec(), after execution is completed.
Does anyone know if I'm just missing something to get an output (running in the browser)?
If I run wget_file.php from the command line, I'll see a display of the wget results
wget doesn't output the file contents to console by default, so presumably you're talking about the status information.
I'd imagine this is output to STDERR rather than STDOUT, and it's STDOUT that shell_exec obtains for you in your PHP script:
when you run your script from the command line, you're still seeing the output because both streams are shared by the single terminal window; it's just that you're seeing it directly from wget and not from your PHP script;
in the case of passing it through Apache and to a browser to satisfy a web request, this terminal context is disconnected from the result the user sees.
In your shell command you can redirect the former to the latter:
$command = "wget http://mydomain.co/media/bigbigbig.wav 2>&1";
The comments on the documentation for shell_exec touch on this, particularly the — er — very first one!

Categories