Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

command

php

exec

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?

like image 949
Yu Guo Avatar asked May 24 '13 11:05

Yu Guo


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

like image 126
urzeit Avatar answered Oct 04 '22 14:10

urzeit


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.

like image 28
Константин Ван Avatar answered Oct 04 '22 14:10

Константин Ван