Can't exceed 8175 chars on commands strings in exec, system and shell_exec PHP functions

I couldn’t locate any information regarding this problem. I’m encountering an issue where I need to write lengthy commands in PHP and execute them. However, when the command exceeds a length of 8175 characters, it fails to execute. Interestingly, if I manually copy and paste the same command into a terminal, it runs without any problems. Here’s a basic example to illustrate the issue:

$longCommand = "echo ".str_repeat("a",8171); // longCommand length = 8176 -> fails
exec($longCommand, $output);

If you reduce the $longCommand string to 8175 characters, it successfully executes. Is there a character limit imposed on commands when they are executed from PHP, and if so, how can I increase or bypass this limit?

>Solution :

Just found out that this is an officially documented limitation with 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.


Leave a Reply