Run .sh file using exec Laravel PHP(使用 exec Laravel PHP 运行 .sh 文件)
问题描述
我正在尝试运行一个 .sh 文件,该文件会将一个 excel 文件导入我的数据库.这两个文件都在公用文件夹内的同一目录中.由于某种原因,exec 命令没有被执行或没有任何错误发生.
I am trying to run a .sh file that will import a excel file to my database. Both files are in same directory inside the public folder. For some reason the exec command isn't being executed or neither any error occurs.
.sh 文件colde:
.sh file colde:
IFS=,
while read column1
do
echo "SQL COMMAND GOES HERE"
done < file.csv | mysql --user='myusername' --password='mypassword' -D databasename;
echo "finish"
在我的 php 文件中,我尝试了以下操作:
In my php file i have tried following:
$content = file_get_contents('folder_name/file_name.sh');
echo exec($content);
还有:
shell_exec('sh /folder_name/file_name.sh');
注意:我可以直接从 gitbash 执行 sh 文件,但是我希望它使用 Laravel 控制器中的函数来完成.我正在使用 Windows 操作系统.
Note: I can directly execute the sh file from gitbash however I want it to be done using function in Laravel controller. I'm using windows OS.
推荐答案
你可以使用 Laravel 中已经存在的 Symfony 的 Process Component http://symfony.com/doc/current/components/process.html
you can use Process Component of Symfony that is already in Laravel http://symfony.com/doc/current/components/process.html
use SymfonyComponentProcessProcess;
use SymfonyComponentProcessExceptionProcessFailedException;
$process = new Process('sh /folder_name/file_name.sh');
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
这篇关于使用 exec Laravel PHP 运行 .sh 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 exec Laravel PHP 运行 .sh 文件


基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01