如何从Laravel控制器执行外部Shell命令? [英] How to execute external shell commands from laravel controller?

查看:96
本文介绍了如何从Laravel控制器执行外部Shell命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从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应用程序运行命令,我建议使用

If you're wanting to run commands from your PHP application I would recommend using the Symfony Process Component:

  1. 运行 composer require symfony/process
  2. 使用 use Symfony \ Component \ Process \ Process;将类导入到文件中
  3. 执行命令:

  1. Run composer require symfony/process
  2. Import the class in to your file with use Symfony\Component\Process\Process;
  3. 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命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆