2014-08-30 35 views
0

我有一个运行于cmd matlab函数的php文件 - >该函数创建.txt文件并将其填充分析结果。然后php文件将这个.txt文件作为一个字符串(从.txt行)发送到数据库(phpmyadmin)。 我的问题是,当matlab仍在写入文件时,php开始从.txt发送信息。 我想用matlab和php知道他的全局变量来解决它,并将其用作Flag确定Matlab何时完成构建必要文件的标志。我想使用窗口注册表,但它非常复杂。有更简单的方法吗?防止文件碰撞PHP与Matlab

非常感谢, 多伦

我的PHP文件:

unlink('test.txt'); 
if(isset($_POST['filepath'])) { 
    $filename = $_POST['filepath']; 
    $inputDir = "C:\\xampp\\htdocs\\login"; 
    $outputDir = "C:\\xampp\\htdocs\\login"; 

    // here php open the matlab function from the cmd: 

    $command = "matlab -sd ".$inputDir." -r phpcreatefile2('".$outputDir."\\".$filename.".txt')"; 
    exec($command); 

    $fileLoc= "".$outputDir."\\".$filename.".txt" ; 
    echo $fileLoc ; 
    echo "The following command was run: ".$command."<br/>"; 
    echo $filename." was created in ".$outputDir."<br/>"; 
    echo " Now the txt file will write in the DB <br/>"; 

//这里我试图检查文件是否存在,如果它不是空的。但它不工作,因为matlab仍然写入文件。

while (1) { 

     if (file_exists("test.txt")){ 
     echo "check1"; 
     if (filesize("test.txt")!= 0) { 
     echo "check2"; 
     $file = file('test.txt'); 
     $sql = "INSERT INTO `ID_5525_Medical_record`(`Data`,`AV_Power`,`Highest_Amp`,`90BW`,`Url_figure`) VALUES ('$file[0]','$file[1]','$file[2]','$file[3]','$file[4]')" ; 
     if(mysqli_query($connection,$sql)) 
     { 
     unlink('test.txt'); 
     echo "the txt file is now in the DB <br/>"; 
     } 
     break; 
     } 
     else { 
     echo "i am going to sleep"; 
     sleep(1); 
     echo "i am awake"; 
     } 
     } 
    } 

`

回答

0

通常情况下,使用COM是执行PHP和MATLAB之间的进程间通信的正确方法(或者一些替代方案,认为一切都足以让这个简单的任务)。

在这里看起来你只需要调用MATLAB一个,就可以修复在命令后附加-wait的行为。这迫使启动程序保持打开,直到matlab关闭,阻止你的PHP脚本在exec($command);。最大的缺点是,你必须为每个函数调用启动Matlab,然后关闭它。使用com,一个实例可以保持开放并且执行一切。