我正在尝试运行一个 .sh 文件,将 Excel 文件导入到我的数据库中。这两个文件位于公共文件夹内的同一目录中。由于某种原因,exec 命令没有被执行,或者没有发生任何错误。
.sh 文件冷:
IFS=,
while read column1
do
echo "SQL COMMAND GOES HERE"
done < file.csv | mysql --user='myusername' --password='mypassword' -D databasename;
echo "finish"
在我的 php 文件中,我尝试了以下操作:
$content = file_get_contents('folder_name/file_name.sh');
echo exec($content);
And:
shell_exec('sh /folder_name/file_name.sh');
注意:我可以直接从 gitbash 执行 sh 文件,但我希望使用 Laravel 控制器中的函数来完成它。我使用的是Windows操作系统。
您可以使用 Laravel 中已有的 Symfony 的 Process 组件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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)