下面的命令在命令提示符下工作。如何使用命令提示符从php执行此操作。
"C:'Program Files (x86)'DBConvert'SQLite2MySQLSync'SQLite2MySQLSync_Cons.exe"/Session:"session"
我按以下方式进行了尝试,但两种尝试都不起作用。你能建议正确的格式吗?
1)exec('C:'Program Files (x86)'DBConvert'SQLite2MySQLSync'SQLite2MySQLSync_Cons.exe "'/Session:session'"');
2)system("C:'Program Files (x86)'DBConvert'SQLite2MySQLSync'SQLite2MySQLSync_Cons.exe /Session:session",$status);
我发现很多解决方案在尝试从提示符执行命令时都有效,但这取决于您使用的窗口版本。这是我通过php运行命令的方式,我知道这些命令可以在命令提示符下运行。你需要在php5.4+中启用COM扩展。我发现,最困难的部分是确保到处都有正确数量的反斜杠。我通常会回显$runCommand,以便在对语法不太正确感到失望之前测试输出。
// -- Windows command prompt action -- //
$WshShell = new COM("WScript.Shell");
$data='C:'Program Files (x86)'DBConvert'SQLite2MySQLSync'SQLite2MySQLSync_Cons.exe /Session:session';
$runCommand = "c:''windows''system32''cmd.exe /C ".$data;
$output = $WshShell->Run($runCommand, 0, false);
// -- end Windows command prompt action -- //
您可能需要查看我专门为此编写的命令库。(对不起无耻的酒吧)
它协调了操作系统的差异,并试图解决一些讨厌的windows错误。它还允许您访问stdout、stdin和stderr,即使在windows下使用exec也是如此。
例如,在您的案例中:
use Tivie'Command'Command;
$cmd = new Command('Tivie'Command'ESCAPE);
$cmd->setCommand('C:'Program Files (x86)'DBConvert'SQLite2MySQLSync'SQLite2MySQLSync_Cons.exe')
->addArgument(
new Argument('/Session:"session"')
);
$result = $cmd->run();
Command::run()
返回一个Result对象,您可以访问该对象来检索命令的结果。
echo $result->getStdOut(); // The Standard Output of the command
echo $result->getLastLine(); // The last line of the Standard Output
echo $result->getStdIn(); // The passed standard input
echo $result->getStdErr(); // The standard error
echo $result->getExitCode(); // The command's exit code