如何在PHP中执行CMD命令
要在PHP中运行CMD命令,可以使用'exec'或'shell_exec'函数。
下面是一个例子:
<?php
$command = 'dir'; // 要执行的CMD命令
// 使用exec函数
exec($command, $output, $return_var);
echo 'Output:
';
echo implode("\n", $output);
echo "\nReturn code: $return_var\n\n";
// 使用shell_exec函数
$output = shell_exec($command);
echo 'Output:
';
echo $output;
?>
在上面的例子中,我们使用'exec'函数和'shell_exec'函数来执行'dir'命令(Windows上的命令,用于列出当前目录中的文件和文件夹)。'exec'函数将命令的输出存储在'$output'数组中,并将返回值存储在'$return_var'变量中。'shell_exec'函数将命令的输出直接返回为一个字符串。
请注意,执行CMD命令可能存在安全风险,请谨慎使用。
原文地址: https://www.cveoy.top/t/topic/Sfd 著作权归作者所有。请勿转载和采集!