我遇到一个问题,我需要在 PHP 中编写冗长的命令并执行它们。但是,当命令长度超过8175个字符时,将无法执行。有趣的是,如果我手动将相同的命令复制并粘贴到终端中,它运行时不会出现任何问题。这是一个说明该问题的基本示例:
$longCommand = "echo ".str_repeat("a",8171); // longCommand length = 8176 -> fails
exec($longCommand, $output);
print_r($output);
如果将 $longCommand 字符串减少到 8175 个字符,则它会成功执行。从 PHP 执行命令时是否有字符限制?如果有,如何增加或绕过此限制?
最佳答案
刚刚发现这是 Windows 的官方记录限制:
The maximum length of the string that you can use at the command prompt is 8191 characters. This limitation applies to: the command line individual environment variables that are inherited by other processes, such as the PATH variable all environment variable expansions If you use Command Prompt to run batch files, this limitation also applies to batch file processing.
关于php - exec、system 和 shell_exec PHP 函数中的命令字符串不能超过 8175 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77258270/