Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

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

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
Операторы исполнения
tinman at robotterror dot com
20-Aug-2007 07:44
Using a backtick script on Linux to make a little directory lister/linker:

<?php
  $output
= `ls -1|fgrep -v index.php |awk '{ print "<a href=" $1 ">" $1 "</a>" }'`;
  echo
"<pre>$output</pre>";
?>

Save as index.php with whatever HTML formatting you desire.

Then, to put this script in the subdirs for recursive listing/linking, do this at the command line:

# find . -mindepth 2 -type d -exec ln index.php {}/ \;

Of course, I'm assuming there is not already a "index.php" file in the subdirs.

A break down of the command line in the backticks:

* ls -1 # list one file per line (note, not "ls -l")
* fgrep -v index.php # exclude the index page
* awk ... # grab each line from ls -1 and wrap with A HREF tags

Have fun!
robert
01-Mar-2006 11:25
Just a general usage note.  I had a very difficult time solving a problem with my script, when I accidentally put one of these backticks at the beginning of a line, like so:

[lots of code]
`    $URL = "blah...";
[more code]

Since the backtick is right above the tab key, I probably just fat-fingered it while indenting the code.

What made this so hard to find, was that PHP reported a parse error about 50 or so lines *below* the line containing the backtick.  (There were no other backticks anywhere in my code.)  And the error message was rather cryptic:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /blah.php on line 446

Just something to file away in case you're pulling your hair out trying to find an error that "isn't there."
cs at kainaw dot com
29-Dec-2005 02:55
After much trouble, I have concluded that the backtick operator (and shell_exec) have a limited buffer for the return.  My problem was that I was grepping a file with over 500,000 lines, receiving a response with well over 100,000 lines.  After a short pause, I was flooded with errors from grep about the pipe being closed.

I have searched, but I cannot find the exact size of the buffer used by the backtick operator and shell_exec.  So, to avoid this error, you must limit the output of your commands (such as using -m with grep).  Through trial and error, you can get the command to run without error.
shakil dot tanvir at gmail dot com
22-Dec-2005 05:25
For passing parameter to a executable doesn't need an executable. Also it may create problem specifically for CGI Bin aplicatioin. Have a look at the following code:

<?php

$parFile
="param.txt";
$parImage="mohona.gif";

$output=`C:\ms4w\Apache\cgi-bin\owtchart.exe $parFile $parImage`;
echo
"<pre>$output</pre>";

?>

In the above code "owtchart.exe" takes two parameters. One is a text file(param.txt) and another is a name of a GIF file where output will be created. It works fine and doesn't need any BAT file!
vdboor at codingdomain dot com
23-Nov-2005 12:10
Note that most OS-es define two channels for file-output, the stdout and stderr (standard out and standard error). To read the data sent to stderr too, include 2>&1 in the backticks.
aaron dot bentley at utoronto dot ca
20-Jul-2003 04: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 10:45
About the french page and french keyboards : backtick is on the 7 key (7
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com
Z058440144362 Z348613067571