简体   繁体   中英

Why does this PHP script with a shell_exec call run from the command line in Windows 10, but not the browser/localhost?

I'm using XAMPP on Windows 10, and trying to run a simple PHP script that uses the shell_exec function to call a simple R script in the same directory. Here's the PHP index.php file:

<?php

    echo shell_exec('Rscript test.R');

And here's the test.R file:

print("Hello, World!")

From the command line (ie, Git for Windows), when I run php index.php in the folder, I get the following output:

[1] "Hello, World!"

However, when I run index.php from the web browser via XAMPP and localhost , I don't see anything on the page. At first, I thought it might be a permissions issue, so I gave Full control access to all users on the folder, but it didn't seem to change anything.

Any ideas on how to get this working from the browser? Thank you.

Found the answer to the problem I was having. Apparently, even though Rscript is part of my Windows 10 PATH variable, when I try to execute an R script via shell_exec in PHP, I have to have the whole path to the Rscript executable (I guess because the PHP script / Apache server doesn't know about the path variable).

I changed the shell_exec function call as follows, and it worked:

echo shell_exec('"C:\Program Files\R\R-4.1.0\bin\Rscript" test.R');

Here's a link to the SO post that helped me figure this out:

shell_exec does not execute from browser requests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM