1

I am trying to integrate matlab with php. So i need to use php to call a matlab function. I am passing file path as argument to the matlab function using exec(). I tried lot of things but couldn't make it work. This is my simple code of php to call function phpcreatefile() in matlab.

<html>
    <body>
        <form action="" method="post">
            Enter a filename <input type="text" name="filepath"><br />
            <input type="submit" /><br />
        </form>
    </body>
</html>

<?php
if(isset($_POST['filepath'])) {
    $filename  = $_POST['filepath'];
    $inputDir  = "C:\\matlab_2015\\bin";
    $outputDir = "C:\\xampp\\htdocs\\software";
    //echo $outputDir;
    $command = "matlab -sd ".$inputDir." -r phpcreatefile('".$outputDir."\\".$filename."')";
    exec($command);
    echo "The following command was run: ".$command."<br/>";
    echo $filename." was created in ".$outputDir."<br/>";
}
?>

P.S : I have already switched the safe mode to off by making safe_mode_exec_dir=Off in php.ini and restarted the xampp. I tried the command in cmd manually and it works properly.

MukulAgr
  • 229
  • 2
  • 4
  • 13
  • 1
    are you receiving an error? did you check the error logs? are you sure your server configuration isn't preventing you from using `exec`? – mister martin Apr 20 '16 at 18:28
  • @mistermartin I dont know how to check.. as I am very new to this things. Can u tell me how to check logs? – MukulAgr Apr 20 '16 at 18:30
  • 1
    basic debugging: `exec($command, $output, $return_code)` and see what the output and return code are. you're simply assuming nothing could ever go wrong - always check for failure, especially when dealing with external resources (e.g trying to exec matlab) – Marc B Apr 20 '16 at 18:31
  • @mistermartin just checked the php error logs and got this '[20-Apr-2016 20:07:28 Europe/Berlin] PHP Parse error: syntax error, unexpected 'C' (T_STRING) in C:\xampp\htdocs\software\test\index.php on line 16' – MukulAgr Apr 20 '16 at 18:33
  • @MarcB at $output i got "Notice: Array to string conversion in C:\xampp\htdocs\software\test\index.php on line 20" and in $return_code I got 'Array1' – MukulAgr Apr 20 '16 at 18:36
  • use `var_dump`, not `echo` – Marc B Apr 20 '16 at 18:39
  • I would verify your double quotes - apostrophes. Problem might come from here – SamyQc Apr 20 '16 at 18:40
  • @MarcB Got this after using var_dump "array(0) { } int(1)" – MukulAgr Apr 20 '16 at 18:41
  • @SamyQc Dint get what u said – MukulAgr Apr 20 '16 at 18:44
  • then there's no output, and matlab's exit code is `1`. you'll have to look up what that means. – Marc B Apr 20 '16 at 18:44
  • @MarcB I have no clue what that means. Is exec working? – MukulAgr Apr 20 '16 at 18:46
  • @MukulAgr Try to echo $command before the exec(); I want to see what's in this variable – SamyQc Apr 20 '16 at 18:46
  • @SamyQc "matlab -sd C:\matlab_2015\bin -r phpcreatefile('C:\xampp\htdocs\software\test.txt')" – MukulAgr Apr 20 '16 at 18:47
  • Running a Windows machine ? Looks like you have mixed the Windows and the Linux command. Also, try shell_exec() instead of exec() – SamyQc Apr 20 '16 at 18:55
  • @SamyQc Yes I am using windows.. Where did I mess up? – MukulAgr Apr 20 '16 at 18:57
  • I'm really not sure, but try to run the file with a syntax like that : Source hkBattousai (http://stackoverflow.com/questions/6657005/matlab-running-an-m-file-from-command-line?lq=1) : (modify $command function) If it works, I'll rewrite the comment as an answer `"C:\\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\\txt.txt');"` – SamyQc Apr 20 '16 at 19:01
  • @SamyQc Nope. That is to run the .m file and not to call function. Syntax is all right. There are 2 probable errors.. 1. there is some path problem like spacing or sometihng 2. Exec not working. – MukulAgr Apr 20 '16 at 19:08
  • @SamyQc I just figured out that exec is working. There is problem in path spacing – MukulAgr Apr 20 '16 at 19:12
  • 1
    @MukulAgr That's what I was speaking about when I said to check the doubles quotes `" "` and `' '` :p – SamyQc Apr 20 '16 at 19:19
  • @SamyQc HAhaha Gotcha.. Now I will tell you the real problem. That command runs only when i go to bin folder of matlab. N that may be the reason why that command isnt working. Any suggestions? – MukulAgr Apr 20 '16 at 19:23
  • Did you try using `shell_exec()` instead of `exec` ? – SamyQc Apr 20 '16 at 19:25
  • @SamyQc I solved it. Thanx for the great help. I just put 2 commands in single command line and connected with logical &. And it worked. ;) Like this `$command = "cd C:\\matlab_2015\\bin & matlab -sd ".$inputDir." -r phpcreatefile('".$outputDir."\\".$filename."')";` – MukulAgr Apr 20 '16 at 19:28
  • 1
    Great, I'm glad you got it working :) – SamyQc Apr 20 '16 at 19:29

0 Answers0