Octave 3.8.0 and PHP

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Octave 3.8.0 and PHP

eykiriku
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:

$cmd = "octave3.8 -qf /.../path_to_file/myscript.m file.txt";
$ex = passthru($cmd, $output);
var_dump($ex);
var_dump($output)

inside the script myscript.m there is:

e = 5;               % too see if it will be printed onto the web page
disp(e);

The result is : NULL int(1)
There is no way to make it work for now, I also tried to use a shell script. The popen instead results in the sentence "resource(4) of type (stream)".

Few keynotes:

The same command from the terminal works fine (octave3.8 -qf /../path_to_file/myscript.m file.txt)
I already tried to put the PHP process to be run by a sudoer
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

vinukn
octave -qf --persist --path script_path --eval("script_name()") >out.txt
or
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.
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

eykiriku
In reply to this post by eykiriku
@vinu k n, thanks for the advice on the security issues regarding the pipes.
besides that i am not sure if i got your hint right, because if i run a php script with:

$cmd = 'octave -qf --persist --path script_path --eval("myscript.m")';
$ex = passthru($cmd, $output);
var_dump($ex);

the result is NULL and without the -qf option no octave startup message is shown.
i hope it's just me that i confused your tip
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

vinukn
Try
 octave -qf --persist --path script_path --eval("myscript()")
instead
octave -qf --persist --path script_path --eval("myscript.m")
or
octave -qf --persist --path script_path --eval("run('myscript.m')")
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

eykiriku
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?
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

vinukn
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.
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

eykiriku
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.5e+12
+-------------------------------------+|||--++++++|||--||||-||||--|||1e+12|+-+||||-||||--||||--||||--|||5e+11|+---+||||-||||--|||--|||||||||0|+
+-----------------+------------------+||||||||||||||||||||||+++++++|||-5e+11
+--------------------------------------------------+||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).
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

vinukn
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

eykiriku
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
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

vinukn
I think Image file path you have given is invalid
try,
print -dpng  "plot_name_with_path.png".
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

eykiriku
Unfortunately this is not working either, same errors
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

Francesco Potortì
In reply to this post by eykiriku
>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.

Sorry, have not followed this thread from the beginning.  Maybe this
example may give you some ideas for what you want to do:
 <http://wnet.isti.cnr.it/software/damatfrc/>

--
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

vinukn
In reply to this post by eykiriku
saveas(1,  "plot_name_with_path.png")
does it works?
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

eykiriku
FROM PHP:
The command passed to shell_exec for example

$cmd = "octave3.8 -q --eval 'cd '/path/to/file/';Myfile;' 2>&1 ";

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.

SAVEAS CASE:
$cmd = "octave3.8 -q --eval 'cd '/path/to/file/'; Myfile; saveas(1, '/save/path/plot.png')' 2>&1 ";

PASSING AN ARGUMENT CASE:
$cmd = "octave3.8 -q --eval 'cd '/path/to/file/'; Myfile /path/to/file/arg_file.txt' 2>&1 ";
or
$cmd = "octave3.8 -q --eval 'cd '/path/to/file/'; Myfile('/path/to/file/arg_file.txt')' 2>&1 ";
or
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?

Thanks in advance
Reply | Threaded
Open this post in threaded view
|

Re: Octave 3.8.0 and PHP

pascha
In reply to this post by eykiriku
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.