2017-03-06 225 views

回答

2

我怎麼能運行在一個Symfony的命令一個簡單的Linux命令?

首先,嘗試執行簡單/普通命令(ls)以查看會發生什麼,然後繼續執行您的特殊命令。

http://symfony.com/doc/current/components/process.html

CODE:

use Symfony\Component\Process\Process; 
use Symfony\Component\Process\Exception\ProcessFailedException; 

$process = new Process('ls -lsa'); 
$process->run(); 

// executes after the command finishes 
if (!$process->isSuccessful()) { 
    throw new ProcessFailedException($process); 
} 

echo $process->getOutput(); 

結果:

total 36 
4 drwxrwxr-x 4 me me 4096 Jun 13 2016 . 
4 drwxrwxr-x 16 me me 4096 Mar 2 09:45 .. 
4 -rw-rw-r-- 1 me me 2617 Feb 19 2015 .htaccess 
4 -rw-rw-r-- 1 me me 1203 Jun 13 2016 app.php 
4 -rw-rw-r-- 1 me me 1240 Jun 13 2016 app_dev.php 
4 -rw-rw-r-- 1 me me 1229 Jun 13 2016 app_test.php 
4 drwxrwxr-x 2 me me 4096 Mar 2 17:05 bundles 
4 drwxrwxr-x 2 me me 4096 Jul 24 2015 css 
4 -rw-rw-r-- 1 me me 106 Feb 19 2015 robots.txt 

正如你可以在上面看到,如果你把這段代碼在你的控制器之一測試目的,ls -lsa列出存儲在下的文件/文件夾web文件夾!

你也可以做shell_exec('ls -lsa');,這也是我有時候做的。例如shell_exec('git ls-remote url-to-my-git-project-repo master');