Reputation: 31
I do not think I want to use exec
or shell_exec
but I need to run command in Linux from PHP script. I use Symfony framework in the project.
I must get listing of directories using ls -la
.
Upvotes: 1
Views: 3075
Reputation: 1044
Use fromShellCommandline to use direct shell command:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
Process::fromShellCommandline('ls -la');
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
Upvotes: 0
Reputation: 2250
Symfony has a Filesystem Component for work with files and directories. However, it does not have the ability to list files in a directory. The scandir function built into PHP can do that for you. This would be better than issuing direct commands to the OS.
Upvotes: 3
Reputation:
Symfony has a Process component to handle native system calls.
See: https://symfony.com/doc/current/components/process.html
An example from the docs:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$process = new Process('ls -la');
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
Upvotes: 4