Home » Php » command – How to use pipe (" | ") in PHP exec?

command – How to use pipe (" | ") in PHP exec?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I can use CMD to execute two commands at the same time using:

command1 | command2

In PHP, I assumed it should work, but it doesn’t:

Shell_exec("command1 | command2 ");

How do I fix it?

How to&Answers:

Try to add braces:

shell_exec("(command1 | command2)");

There is a comment in the PHP documentation that does some I/O-redirection with popen.

Answer:

Try disabling safe mode.


For shell_exec():

Note:
This function is disabled when PHP is running in safe mode.


For system():

Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable.

Warning:
With safe mode enabled, the command string is escaped with escapeshellcmd(). Thus, echo y | echo x becomes echo y \| echo x.

Answer:

You could try ; instead of | in your command.
Referencing to this comment at php.net.