简体   繁体   中英

Why can't I run the composer script from PHP using exec, shell_exec, system?

I can run about any binary but not that composer.phar using PHP's exec or shell_exec .

In the same folder I have composer and another php executable with same permissions:

ls -lh
total 1,8M
-rwxr-xr-x  1 me me 1,8M août  22 20:48 composer.phar
-rwxr-xr-x  1 me me   39 août  22 21:05 test.php

Test.php contains:

#!/usr/bin/env php
<?php
print 'hello';

I then have this script:

<?php
print $cmd  = "composer.phar --version 2>&1" ;
print "<br>";

$return  = exec( $cmd );
var_dump($return);

print "<br><br>";

print $cmd  = "test.php 2>&1";
print "<br>";

$return  = shell_exec( $cmd );
var_dump($return);

Here is what I get:

composer.phar --version 2>&1
[...]Process.php:81:string 'sh: 1: : Permission denied' (length=26)

test.php 2>&1
[...]Process.php:88:string 'hello' (length=5)

Why do I get that string 'sh: 1: : Permission denied' error? I tried executing in PHP with /usr/bin/env php composer.php /usr/bin/php composer.php , I get the same error.

I solved it by disabling the xdebug extension.

From the doc:

To improve performance when the xdebug extension is enabled, Composer automatically restarts PHP without it.

So I guess that this "PHP restart" is an issue when calling the binary/phar from PHP.

It is be possible to use the environment variable COMPOSER_ALLOW_XDEBUG to get it working with xdebug, also disbaling a few xdebug options that could alter performances:

<?php
$result = shell_exec('COMPOSER_ALLOW_XDEBUG=1 /usr/bin/env php -d xdebug.remote_enable=0 -d xdebug.profiler_enable=0 -d xdebug.default_enable=0 composer.phar --version 2>&1');

我认为它将与system('php /usr/local/bin/composer install -d /...') ,因为如果您直接在shell中运行composer,则可以通过阅读开头的shebang来工作。表示应由php执行的文件,但是使用system()时没有外壳程序,因此需要自行指定。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM