ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹ выполнСния

PHP ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ ΠΎΠ΄ΠΈΠ½ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ выполнСния: ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹Π΅ ΠΌΠ°ΡˆΠΈΠ½ΠΎΠΏΠΈΡΠ½Ρ‹Π΅ апострофы, ΠΈΠ»ΠΈ ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹Π΅ ΠΊΠ°Π²Ρ‹Ρ‡ΠΊΠΈ, β€” ``. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ это Π½Π΅ ΠΎΠ΄ΠΈΠ½Π°Ρ€Π½Ρ‹Π΅ ΠΊΠ°Π²Ρ‹Ρ‡ΠΊΠΈ! PHP попытаСтся Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ строку Π² ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹Ρ… апострофах ΠΊΠ°ΠΊ ΠΊΠΎΠ½ΡΠΎΠ»ΡŒΠ½ΡƒΡŽ ΠΊΠΎΠΌΠ°Π½Π΄Ρƒ; PHP Π²Π΅Ρ€Π½Ρ‘Ρ‚ Π΄Π°Π½Π½Ρ‹Π΅ Π²Ρ‹Π²ΠΎΠ΄Π°, Π° Π½Π΅ просто сбросит Π΄Π°Π½Π½Ρ‹Π΅ Π² ΠΏΠΎΡ‚ΠΎΠΊ Π²Ρ‹Π²ΠΎΠ΄Π°; Π²Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΡΠ²Π°ΠΈΠ²Π°ΡŽΡ‚ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ. ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ выполнСния Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π½ΠΎ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ shell_exec().

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ выполнСния

<?php

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

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

ΠžΠ±Ρ€Π°Ρ‚Π½Ρ‹Π΅ ΠΊΠ°Π²Ρ‹Ρ‡ΠΊΠΈ нСдоступны, Ссли Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ shell_exec() ΠΎΡ‚ΠΊΠ»ΡŽΡ‡ΠΈΠ»ΠΈ.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Π’ ΠΎΡ‚Π»ΠΈΡ‡ΠΈΠ΅ ΠΎΡ‚ ряда Π΄Ρ€ΡƒΠ³ΠΈΡ… языков, ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹Π΅ ΠΊΠ°Π²Ρ‹Ρ‡ΠΊΠΈ Π½Π΅ нСсут ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½ΠΎΠ³ΠΎ значСния Π²Π½ΡƒΡ‚Ρ€ΠΈ строк Π² Π΄Π²ΠΎΠΉΠ½Ρ‹Ρ… ΠΊΠ°Π²Ρ‹Ρ‡ΠΊΠ°Ρ….

Бписок измСнСний

ВСрсия ОписаниС
8.5.0 ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ выполнСния устарСл; ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹Π΅ ΠΊΠ°Π²Ρ‹Ρ‡ΠΊΠΈ большС Π½Π΅ Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‚ ΠΊΠ°ΠΊ синоним Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ shell_exec().
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 3 notes

up
158
robert ΒΆ
20 years ago
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."
up
123
ohcc at 163 dot com ΒΆ
9 years ago
You can use variables within a pair of backticks (``).

<?php
    $host = 'www.wuxiancheng.cn';
    echo `ping -n 3 {$host}`;
?>
up
4
paolo.bertani ΒΆ
3 years ago
If you want to avoid situations like the one described by @robert you may want to disable `shell_exec` and -as a consequence- the backtick operator.

To do this just edit the `php.ini` file and add `shell_exec` to the `disable_functions` setting:

    ; This directive allows you to disable certain functions.
    ; It receives a comma-delimited list of function names.
    ; https://php.net/disable-functions
    disable_functions = "shell_exec"

Then you can still use `exec()` to run terminal commands.