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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题