Calling a matlab exec with php script?

8 views (last 30 days)
veysel
veysel on 7 Feb 2012
Edited: Fred on 4 Oct 2013
Hello,
I have an executable file which is generated by matlab. This file runs a simple calculation and produces a plot (and saves that plot as a bmp file). The exec works fine when used stand-alone (i.e. when I double click). However the problem arises when I try to call the exec from a php script. The calculation still runs and I get the output but the plots dont get generated. When I search the folder wher the plots are supposed to be stored at, I see the file but they are all corupted (bmp file with 1kb size and cannot be seen).
As I said, the calculation still runs when called from the script, just the plots get corrupted and I dont know why.
I would appreciate if any of you has an idea?
PS: I am using the below script for calling the exec
<?php
exec("filename.exe");
?>
  1 Comment
Kaustubha Govind
Kaustubha Govind on 8 Feb 2012
Some of us may not be familiar with PHP - could you maybe just try running the executable from the system shell and check if you see the same symptoms?

Sign in to comment.

Answers (3)

Jason Ross
Jason Ross on 9 Feb 2012
Try running something simple like
system('set')
this will tell you the environment you get when running from command prompt, from the MATLAB desktop and from within PHP. Look for differences there. If the PHP environment is different, try and make it the same as the others by setting environment variables and see if this makes a difference.

veysel
veysel on 16 Feb 2012
Hi everyone, First of all, thank you very much for your efforts in solving this problem that I have encountered. I am happy to say that I finally found the solution (although I am really surprised about the way it works but still..better than not having a solution at all).
No matter what I tried (and this has nothing to do with the environment or the system etc...) the plots get corrupted when the exec was called via php script (again, only the images, the calculations still run fine and results got published)...then I decided to further investigate the way I am saving the images. I was originally using the command "save as" in the actual m file...and with a desperate attempt I tried using "print -djpg" ...and surprisingly enough it worked just fine..the plots no longer get corrupted when the exec file is invoked via php.
I just wanted to post this if anyone else is facing the same problem...I still dont know why and how the files get corrupted via save as command..might have something to do with w7 64kb..I don't know...but this is at least how I fixed it.
Thanks again

Jason Ross
Jason Ross on 8 Feb 2012
Which user context is this script executing as? If this is part of a web server, it's possible that it could be LocalSystem or IIS_USER. These users may not have access to a desktop, so if you are doing something that uses something with reliance upon screen geometry, they may be returning nonsense.
Look at what the exec() call is doing in PHP. Is it simply calling a command shell, or is it executing the command using some other method? The environment provided by the exec() may be missing some things that are required by your plot.
You may want to consider creating a debug log where you write the values of things when running under the php context as well as the command window. It's likely that there will be some difference that's causing the failures.
  1 Comment
veysel
veysel on 8 Feb 2012
Thank you for your answer however, the system I am using is a local system (and I call the exec from my localhost) so I don't think it should be a problem of either access or screen size
especially considering I am running in my own system and it shouldn't be problem with the environment although i am not 100% sure on what you by that.
Also, I did run it from the system shell and it works
just like when it works when I double click on it .

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!