Can I call python script or function from php - php

I have the php page which lists the backups with filename and username.
the backups are done with python script but for web interface I used php.
Now I want to put the facility on webpage where there will be button called restore next to backup file name and all the home dir contents are replaced.
I can make the python function with parameters like backup filename , username and it restores the backup.
But I don't know how can I pass variables to python via php

Yes you can call exec("python pythonscript.py someparams"); or if you need to save the output to a variable use this code
exec("python mypythonscript.py someparams",$output);
var_dump($output);
More informaiton: call python script by php and solve errors

You can call the shell from PHP:
shell_exec()
exec()
backticks
and there are more.

Related

Run a php script in the background

I am trying to run a php script in the background on windows.
Basically my goal is to run a exe file in the background and receive a signal when its done.
I'm open to any suggestions.
Thanks.
make a .bat file and on that bat file reference php file then it will work
if your using linux environment, i suggest creating a bash file and put your commands.
and if your using windows environment, use .bat file and put your commands.
php filename.php <arguments>
php another_filename.php <arguments>
and then run in in the terminal.. for the signal, you can exit() your php script based on what the condition your script needs..
UPDATE
Regarding your comment, if you want the webpage to create an executable then you must use the backtick or exec() in your web php file
for example, in your web php file:
//other php codes
echo `php runthisfile.php`;
echo exec('php runthisfile.php');
//other php codes
then if this is encountered, in the background, it will run the runthisfile.php file.. the backtick is a special symbol in php and exec() is a function that enables you to execute command line instructions..
and regarding the signalling, there are many approach.. some are:
in the background file, you can store a value in the database that the web page is listening to
make a soap call that uses xml as its data medium
or if what I think is right, just use AJAX in your web.. it really is helpful

Octave script through php windows

I am trying to run an octave script through PHP. I already googled and found some results but none of them are working for me. I tried with exec() and system(). I even created a batch file which calls 'octave myScript.m" and called this bat file using system() of PHP but it doesnt seem to work. In the browser page I am just seeing 'C:/FOLDER_PATH>octave myScript.m". The octave script simply creates a new file and writes some text to it. When i directly run the bat file (by double-clicking on it), the file is getting created properly. I also added folder path to octaverc file but it doesnt seem to work. I need to do some image processing in octave for which I already wrote the script. I need to invoke this script on a client request and send the result to back the client. I am checking the invocation process through a sample script which as I mentioned earlier creates a new file. What am I doing wrong?
My php code is this:
$cmd = "cmd /c C:\PATH_TO_BAT_FILE\myBat.bat";
exec($cmd,$output);
system($cmd);
echo implode ("\n",$output);
Note that my path contains double backslashes to avoid escape sequence characters
My bat file is this
octave temp.m
My octave code(temp.m) is this
fid = fopen("helloScript.txt",'w');
fprintf(fid,"Hello world!");
fclose(fid);
Ouput on the webpage is this:
C:\PATH_TO_BAT_FILE>octave temp.m C:\PATH_TO_BAT_FILE>octave temp.m
I can see in the task manager that a new process is getting created whenever I run the PHP script in browser (I am guessing that it is cmd).
Also, when i change my bat file to
echo hello
I am able to see the following in my browser page
C:\PATH_TO_BAT_FILE>echo hello hello C:\PATH_TO_BAT_FILE>echo hello hello
So this could mean that the bat file is getting executed properly. But when I replace the bat file script with 'octave MY_FILE.m' I am not able to see the output. It may mean that my octave is not configured properly? or is there something I am missing?
Any help would be appreciated.
Thanks
If you are going to run the batch file to create it in php then the php command should be like this.
exec('cmd.exe /c C:\path\to\test.bat');
This is embarassing but I solved it by giving full path. In the bat file I specified the complete path of the octave.exe (C:\Software\PATH_TO_OCTAVE.EXE) and the complete path of the '.m' file.
In my php, I just used exec()

Generate an output file with python inside a php script

I need help in figuring out on how to generate an output file in python inside php script.
So here is the problem:
I have a php script that lets user upload files.
Those files are stored in the server.
After that those files will be run on a python script, and will generate output files.
After that the php script will get those output files, and send an email to the user.
I want to make it automatically send to the user after they upload and click the submit button.
I have all those done, until trying to call a python script inside the php script
The python script inside the php script does not execute the function where it generates the output files.
This is the function that I use for calling the bash script:
system('bash /data/home/rxs061/public_html/test.sh');
I also have used the function exec(); and shell_exec();
But does not work.
The problem here, the function does not run on the command line, so the output file won't be generated.
This is inside the bash script (test.sh):
python /home/rxs061/public_html/test.py
outfile=$PWD/test.out
echo "$PWD/"
The problem here it does not want to generate "test.out"
This is inside the the python script (test.py):
import sys
outfile = open('test.out', 'w')
outfile.write('it worked\n')
outfile.close()
This does not have to do with permissions.
How do you put this all together in a single php script?
Or are there any other solutions?
Or how to make it output files???
Oh I get all fix, it something to do with the permission.
After I do chmod 777, it is all fixed

Linux command populate file from php script output

I have a PHP script which generates some complex XML output. The XML is currently being output as a webpage but instead of a webpage I need it as a physical file on my server.
So, I wonder if there is a way to pipe the output of my PHP script into the Linux touch command so that my PHP output populates, or overrides, a file on my server?
I am currently trying the following code without success:
touch test | php xml_reports.php
You can actually run PHP from the command line.
php filename.php
So if your php file consisted of:
<?php
echo "Hello World";
?>
The output would be "Hello World" (without the quotes) in your terminal. So if you were writing an application in PHP, try and think about how you would approach it without the browser rendering your output. I hope that helps.
[edit]
You might also want to look into shell_exec. (http://php.net/manual/en/function.shell-exec.php)
Assuming I understand your question correctly, here is a solution. In this case however, rather than piping the PHP output, the PHP code itself outputs a copy of the file on the server. I'm not sure how vital it is that this is done through a linux pipe. Also note that this solution is copied from http://forums.codewalkers.com/showpost.php?p=40350&postcount=3.
1) at the very start of your script put this line :
ob_start();
2) at the very end of your script, put this:
$page = ob_get_contents();
ob_end_flush();
$fp = fopen("output.html","w");
fwrite($fp,$page);
fclose($fp);
And, bam, you should have the whole page in a file called output.html.
Now, one thing you need to be sure of is that the webserver server can
write the file output.html. That means you need to make sure the
webserver has the permissions to write in whatever directory you plan
to store that file in.
How does it work? Basically, it just buffers all output from your
script. Then, you store that buffer into a string. Then you send the
buffer to the browser. Then you store that string in a file...

How can i return and process results from running a .bat file from PHP

I have a PHP script that executes a .bat file using
system("cmd /c C:\dir\file.bat");
This launches an AWS server and returns info such as the id of the server started. I need to use this id in the script later on. How can I return the results from the .bat file to PHP and then how can I extract the id from the rest of the results. Is the returned data simply a string that i need to slice to get the bit i need?
I will then run a .bat file that executes the following -
ec2-associate-address -i i-######id ip.###.###.###
Thanks all
You can capture the output of a system command by using the exec() function or passthru() function.
Instead of system() you can use proc_open(). That way you can read the output of your .bat file by reading the stdout pipe. You then get your simple string that you can slice 'n dice to get your process ID.

Categories