如何在php中自动运行Windows cmd

问题描述 投票:-1回答:2

我已经准备了这个php webservice

<?php
$user_name = "root";
$user_pass = "";
$host_name = "localhost";
$db_name = "dbupload";

$con = mysqli_connect($host_name,$user_name,$user_pass,$db_name);

if ($con) {
  $image = $_POST["image"];
  $name = $_POST["name"];
  $sql = "insert into imageinfo(name) values ('$name')";
  $upload_path = "B:/Smart_Music/Emotion_Model/images/$name.jpg";

  if (mysqli_query($con,$sql)) {
    file_put_contents($upload_path,base64_decode($image));
    $reponse=array();
        $reponse["response"]="Image Uploaded Successfully";
        echo json_encode($reponse);
  }  
  else {
    $reponse=array();
   $reponse["response"]="Failure";
   echo json_encode($reponse);
  }
}
else {
  $reponse=array();
 $reponse["response"]="Failure";
 echo json_encode($reponse);
}
mysqli_close($con);

?>

我想要实现的是在发送响应后自动执行一些Windows命令。

这是命令:

cd /d B:\Smart_Music\Emotion_Model\src
activate Emotion
python image_emotion_demo.py ../images/Selected_Photo.jpg
php windows cmd
2个回答
0
投票

您可以使用exec or shell_exec

<?php 

// Use ls command to shell_exec 
// function 
$output = shell_exec('ls'); 

// Display the list of all file 
// and directory 
echo "<pre>$output</pre>"; 
?> 

输出将是对我来说

gfg.php
index.html
geeks.php

为了安全起见,请确保未在本地计算机上禁用此功能


0
投票

我尝试过了,正如您提到的,但是没有用系统中没有发生移动到新目录的情况,该目录显示htdocs文件夹的内容

$command_one='cd /d B:\Smart_Music\Emotion_Model\src';
shell_exec($command_one);
$output = shell_exec('dir');
echo "<pre>$output</pre>";
© www.soinside.com 2019 - 2024. All rights reserved.