我怎么能运行在一个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');