Generate an output file with python inside a php script - php

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

Related

Execute a windows shortcut from php

I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec 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.
Please refer this link for your version of PHP: manual

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()

How can I have a php script call a perl script that requires a parameter?

I have a php script that allows people to upload files to a server, and I have a perl script to convert the filetype to something more useful for data extraction. The php script saves the name of the uploaded file as $filename . In order to run, the converting script needs to be passed the name of the file to be converted. I was able to call a test script and pass it the name of the uploaded file in the following way
exec("/usr/bin/perl /full/path/to/mytestscript.pl $filename");
This test script executed all of the commands that I needed it to. However, this did not work with my converting script. The code to keep the variable is the same in both the test script and the converting script.
I can run the converting perl script fine from the terminal, but I want to run it from the php script. I run the converting perl script from the terminal by navigating to the directory holding both the file to be converted and the converting perl script.
perl my_converting_script.pl file_to_convert.TCX
Any advice on how to have the converting script run from the php script instead of the terminal would be greatly appreciated!

Can I call python script or function from 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.

Categories