Операторы исполнения

PHP поддерживает один оператор исполнения: обратные кавычки (``). Обратите внимание, что это не одиночные кавычки. PHP пытается выполнить строку, заключенную в обратные кавычки, как консольную команду, и возвращает полученный вывод (т.е. он не просто выдается на выходе а, например, может быть присвоен переменной). Использование обратных кавычек аналогично использованию функции shell_exec().

<?php
$output
= `ls -al`;
echo
"<pre>$output</pre>";
?>

Замечание: Обратные кавычки недоступны, в случае, если установлен безопасный режим или отключена функция shell_exec().

Ознакомьтесь также со следующими разделами документации: Функции для выполнения программ, popen() proc_open(), и Использование PHP в командной строке.



add a note add a note User Contributed Notes
Операторы исполнения
aaron dot bentley at utoronto dot ca
20-Jul-2003 07:45
waylanator's example can be dangerous, since it doesn't prevent characters with special meaning from being emitted to the commandline.  Programming errors or untrusted data could cause serious problems.  At the bare minimum, remove all non-alphanumeric characters before passing a string to the shell.  escapeshellarg() is also useful in *nix environments, but usually the best approach is to bypass the shell, using exec() etc.
thierry_bo at php dot net
23-May-2003 01:45
About the french page and french keyboards : backtick is on the 7 key (7 `), not the english pound key ( $ ).
dexter at ragehosting dot net
28-Mar-2003 02:18
Or just %* (i think) to pass ALL variables specified
waylanator no at spam hotmail dot com
14-Feb-2003 03:03
Want to pass a parameter with your batch file to the executable?
Just do this:

mybat.bat:
__________
@echo off
c:\progra~1\myprog~1\program.exe %1
__________

php:
__________
<?php
$par
= "my_parameter";
$test=`c:\mybat.bat $par`;
echo
"<pre>$test</pre>";
?>
__________

Have more than 1 parameter?  Just add %2 %3 %4 and so on in the batch file.

Hope this helps someone.
waylanator no at spam hotmail dot com
14-Feb-2003 12:12
In Windows it appears you can only call an executable file that resides in the system path which is defined by Windows.  As a workaround you can place a batch file in the system path that calls the program from it's dir. Just make sure to use short MS-DOS file and dir names.
For example:
If you were calling the file c:\program files\my program\program.exe do this:

mybat.bat look like this:
_________
@echo off
c:\progra~1\myprog~1\program.exe
_________

Save mybat.bat in c:\ or c:\windows or any other dir in the system path as defined by windows.

Then in php call the batch file:
_________
<?php
$test
= `c:\mybat.bat`;
echo
"<pre>$test</pre>";
?>
_________

That should do it.
Of course this will only work for a program you can run from the MS-DOS command prompt, but (as I understant it) that goes for any executable you call with PHP anyway.
Tested in Win98 running Apache 1.3.27 and PHP 4.3.0
bunny at bytech dot fi
03-May-2002 04:32
ignore_user_abort(true);
might prevent the situation described above from taking place.
reed-NO at SPAM-zerohour dot net
13-Mar-2002 03:45
When a program is run using backticks, and the user cancels page loading (if your program is taking too long!), the shell running the program (the one in the backticks) may continue indefinitely on the server. I do not know if this is a bug, or just a danger of using this feature.  (It may depend on the way the browser "cancels" the request -- it was a problem on both IE and OmniWeb for the Mac).  Beware!