Consider the following architecture:
- users upload data into a database
- my web app accesses the database, gets the users data, processes it and sends back the results to a webpage.
The smartest solution I came up to achieve this is by using PHP, mysql, apache and Octave 3.8.0 on Ubuntu 12.04.
No user input is required and the script flow that I thought to use is:
- PHP uses mysql to retrieve the users data
- Octave is called from PHP to do all the processing
- The results are sent to a webpage or saved into another file.
The problem occurs when calling Octave from PHP with the functions exec(), passthru(), ...
All these functions let only to initialize Octave and I can say it because I see the startup message on the webpage and then no octave script is executed. Here below there is the code I am using:
The set up PHP, Apache works yet (the Apache user is www-data). The problem occurs related to the following call:
octave -qf --persist --path script_path --eval("script_name()") >out.txt
octave -qf --persist --eval("run('script_with_full_path')") >out.txt
This method is only useful in non interactive mode. Set graphics toolkit as gnuplot,since exec function works with pipes. Again note that your m script does not contain file or system related octave commands.
The problem is solved by putting all scripts into the same folder. And that's acceptable.
Your commands were right as my old one is, the problem seemed to be that I cannot execute scripts from different folders. That is, my virtual host points to a folder, let's name it 'Doc'. Both scripts, the Octave's and the PHP ones are inside 'Doc', but into two different folders. This is what blocked all my calls to a specific octave script even when including the complete paths.
Regarding the "gnuplot" thing: if I call Octave from PHP and the Octave script loads first the gnuplot toolkit, and then there is e.g. plot([1:20]) command, nothing happens. No figure is shown onto the webpage. Do I have to save the plots on Octave and then load them back in PHP or what's a fair way to do it?
octave -qf --persist --eval("graphics_toolkit gnuplot;cd('script_path');myscript()")
I think you need -i after -qf inorder to plot. Better idea is save plot and display in webpage.
Try octave -qf --persist --eval("graphics_toolkit gnuplot;cd('script_path');myscript();saveas(1,'image_file_name' ")
In order to save plot you need third party tools like gs.
1- the first command you suggest works only from the terminal (rearranging it a bit). But the same .m script cannot be executed from PHP instead (maybe any permissions issue?) and it was the same behavior i experienced with your previous suggestions
2 - The octave plot and print functions display on the web page an output not formatted like:
+--------------------------------------------------+||1 1.5 2 2.5 3 3.5 4 |
if i use the --persist option along with the -i option, both functions (plot and print) will result in 2 identical sequences like the one here above are shown on the webpage.
3- the option -f blocks my pkg loading command from the octave init file, but at the same time no plot is shown if there is no such option when i call it from PHP. Is there a way to let Octave load the default pkgs without causing this downside of blocking any plot to the webpage? I need the imfilter function of the image pkg and the Octave script blocks at that point when called from PHP (even if imfilter seems not to work in either case).
thanks but i already know both links, the thing is that the project i am working on will be released commercially in the end and there are no guarantees that none of the web octave solutions will last for long, will be updated often and there is no guarantee also about the server downtime and performance. I thought that a server with octave directly installed would solve all these uncertainties.
i've solved the permissions errors. they where related to the fact that the packages were installed locally. when i reinstalled them all in a global way, no such packages related problems occur anymore.
regarding the plot / print problem, i've found that by using the print -dpng -debug plot.dpng mode i am to determine that the following errors occur:
error: fputs: invalid stream number = -1 error: called from: error: /opt/octave3.8/share/octave/3.8.0/m/plot/util/__gnuplot_drawnow__.m at line 315, column 5 error: fclose: invalid stream number = -1 error: /opt/octave3.8/share/octave/3.8.0/m/plot/util/__gnuplot_drawnow__.m at line 65, column 9 error: /opt/octave3.8/share/octave/3.8.0/m/plot/util/private/__gnuplot_print__.m at line 192, column 5 error: /opt/octave3.8/share/octave/3.8.0/m/plot/util/private/__gnuplot_print__.m at line 176, column 5 error: /opt/octave3.8/share/octave/3.8.0/m/plot/util/private/__gnuplot_print__.m at line 123, column 7 error: /opt/octave3.8/share/octave/3.8.0/m/plot/util/print.m at line 420, column 14
>thanks but i already know both links, the thing is that the project i am
>working on will be released commercially in the end and there are no
>guarantees that none of the web octave solutions will last for long, will be
>updated often and there is no guarantee also about the server downtime and
>performance. I thought that a server with octave directly installed would
>solve all these uncertainties.
is executed properly but the print function (inside Myfile.m), still not work. I got the same errors like the previous post...error: fputs: invalid stream number = -1 etc....
The same call from the terminal works properly till the end, hence a plot pops up and then an image called plot.png is correctly saved to the desired path.
Regarding your suggestion of using the saveas command. Whenever I add the saveas command the output is:
parse error: syntax error >>> /path/to/file/....
In my case I need also need to pass a parameter (a string) to the Myfile, and whenever I use the --eval option the error arises again.
The command I executed are here below.
PASSING AN ARGUMENT CASE:
$cmd = "octave3.8 -q --eval 'cd '/path/to/file/'; Myfile /path/to/file/arg_file.txt' 2>&1 ";
$cmd = "octave3.8 -q --eval 'cd '/path/to/file/'; Myfile('/path/to/file/arg_file.txt')' 2>&1 ";
passing a variable containing the string :'/path/to/file/arg_file.txt'
All this could be solved if I am able to make PHP execute the same commands I can execute from the terminal. And so avoiding the --eval function or (of course understanding what the syntax error is).
Since the commands run fine from the terminal (as root), I am guessing that could be that PHP doesn't have the privileges to open a plot from Octave and save the figure to a folder (the user I use in PHP has root privileges). Could it be?
Maybe this is too late but if you're running Ubuntu 12.04 just install and use octave-3.2 from its current repository. I don't exactly know what the problem is but it should work. I had the same issue.