Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);
print_r($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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Source

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading