使用exec Laravel PHP运行.sh文件 [英] Run .sh file using exec Laravel PHP

查看:62
本文介绍了使用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文件库:

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

you can use Process Component of Symfony that is already in Laravel http://symfony.com/doc/current/components/process.html

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

$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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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