dqzlqfqeh845799833 2018-12-19 21:21 采纳率: 0%
浏览 47
已采纳

从php Web应用程序执行节点脚本(Windows)

I have a php web application and on one of the route I try to execute node script.

Node script location: C:\Users\meusr\Workspace\www\myproject\directImport.js

I have configured my package.json, such that npm start executes above script.

php (laravel) application's executable index.php located at: C:\Users\meusr\Workspace\www\myPHPproject\public\

And on one of my route: importer/run

I have:

$commandToRun = "npm start --prefix ". env('IMPORTER_PATH'). " /dev/null 2>&1";

Where IMPORTER_PATH is configured in my env, as: IMPORTER_PATH = C:\Users\meusr\Workspace\www\myproject\

when I try to execute the command with exec

exec($commandToRun, $output);

Page keeps on loading no output, no error and no end.

The script has no issue and running the same command on command line on windows system works:

npm start --prefix C:\Users\meusr\Workspace\www\myproject\

outputs as expected.

I thought it was issue with permission first (which should have thrown error, but I still tried and ran another command)

exec('npm -v', $output);

which outputs my npm version.

Similarly I tried to use simply node directImport.js which didn't work either.

Then tried to change directory to the location where node file is located and ran the command again. exec(cd C:\Users\meusr\Workspace\www\myproject\ && dir /dev/null 2>&1) // this works, but:

exec(cd C:\Users\meusr\Workspace\www\myproject\ && node directImport.js /dev/null 2>&1) // this didn't work

  • 写回答

1条回答 默认 最新

  • dongxiejie9387 2018-12-28 12:17
    关注

    Thanks to miken32, what worked for me was:

    <?php 
    $commandString = 'start /b c:\\programToRun.exe -attachment "c:\\temp\file1.txt"'; 
    pclose(popen($commandString, 'r')); 
    ?>
    

    http://php.net/manual/en/function.popen.php#92413

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?