问题描述
我需要从controller执行shell命令,但不仅要对项目中的文件执行ex.系统('rm/var/www/html/test.html')或系统('sudo解压缩/var/www/html/test.zip');
I need to execute shell commands from controller , but not only for files inside the project , ex. system('rm /var/www/html/test.html') or system('sudo unzip /var/www/html/test.zip');
我调用了该函数,但是没有任何反应,是否知道如何从控制器执行外部shell命令,例如删除另一个目录中的一个文件?
I call the function but nothing happen , any idea how to execute external shell commands from controller like removing one file in another directory?
system('rm /var/www/html/test.html');
//or
exec('rm /var/www/html/test.html')
推荐答案
如果要从PHP应用程序运行命令,我建议使用 Symfony Process组件:
If you're wanting to run commands from your PHP application I would recommend using the Symfony Process Component:
- 运行
composer require symfony/process
- 使用
use Symfony \ Component \ Process \ Process;将类导入到文件中
-
执行命令:
- Run
composer require symfony/process
- Import the class in to your file with
use Symfony\Component\Process\Process;
Execute your command:
$process = new Process(['rm', '/var/www/html/test.html']);
$process->run();
或者,(如果运行php的进程具有正确的权限),您可以简单地使用PHP的unlink()函数删除文件:
unlink('/var/www/html/test.html');
这篇关于如何从Laravel控制器执行外部Shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!