我有一个运行于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";
}
}
}
`