0

I'm trying to execute a php script from a php file, but it seems that i can't get the correct bin path of the php.exe. Its not doing anything as far as i can tell. Lets assume service-oauth.php is a simple echo.

EDIT3: I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented.

EDIT2: I tried calling the script directly from the server console as @Dagon suggested and it works, both using the php env variable and the php path to the bin, its clear that the path is correct, but something is preveting to get the output or to run the script using the php exec() function

service-oauth.php

<?php echo "Hello there"; ?>

And this is my script:

   $basePath =  plugin_dir_path( __FILE__ ); # Wordpress function, asume it works.
   $fileToExc = $basePath . 'service-oauth.php';
   # PHP_BINDIR: /usr/local/bin
   # PHP_BINARY: /usr/bin/php
   # exec("which php") /usr/bin/php
   $phpPath = exec("which php");
   $output = exec("$phpPath $fileToExc");
   print_r($output);

There are a lot of answers on stackoverflow that recommend any of those 3 options i commenented in the code, but none of them seems to work, not sure if it is the path or something else that is not working. I've tested this script on my localmachine (windows) and it works (even though i had to use a hardcoded path to the bin since i wans't getting the correct path), but i'm testing on my production server (linux) and is not working.

Note: Let me be clear, that this is not a duplicate, i've tried the following answers in these posts and many others, and it didn't work for me:

How to get path of the php binary on server where it is located

Can't execute PHP script using PHP exec

PHP exec to run a file

How to call shell script from another shell script?

Execute a PHP script from another PHP script

I've also tried using .exe at the end of the binary(windows localmachine), using php-cli instead of php, and i've tested the excec function and it works, but not in this case. Also tried with a shebang in the called script. It's propably something simple that i'm not aware of but i've been spending a lot of hours searching and testing and nothing so far. Any help is appreciated.

EDIT:

Using scandir on the bin folder shows scandir("/usr/bin/")

Array(
 ...
 [632] => php
 [633] => php-cgi
 [634] => php-cli
 ...
)

Tested if in safe mode using ini_get('safe_mode'), but it seems off.

Community
  • 1
  • 1
Lu Roman
  • 2,220
  • 3
  • 25
  • 40
  • im most cases all you need is `$output = exec("php $fileToExc");` –  Oct 13 '14 at 01:44
  • Sry i forgot to mention it was the first thing i tried, but thanks =S – Lu Roman Oct 13 '14 at 01:45
  • starting from there was the error ? does "php filename" work on the command line? –  Oct 13 '14 at 01:45
  • No errors, just not output – Lu Roman Oct 13 '14 at 01:46
  • http://stackoverflow.com/questions/3889486/how-to-get-the-path-of-the-php-bin-from-php – Will Oct 13 '14 at 01:48
  • @quasivivo already tried those options, including $_SERVER['_']. The path output is in my code comments, i think the path might be correct since is the usual path on linux, but no idea why there is no output – Lu Roman Oct 13 '14 at 01:51
  • does it work in the command line ? –  Oct 13 '14 at 01:57
  • Is there some reason why you MUST use `exec()`? If it's a PHP file why not just call it over HTTP? – I wrestled a bear once. Oct 13 '14 at 02:00
  • 1
    Did you check that safe mode is off and/or exec is enabled in your php.ini? http://www.cyberciti.biz/faq/linux-unix-apache-lighttpd-phpini-disable-functions/ – Will Oct 13 '14 at 02:00
  • Also, if you're not even getting your output via command line then there's probably something going on other than a PHP issue.. perhaps your path's are wrong.. – I wrestled a bear once. Oct 13 '14 at 02:02
  • @quasivivo i've tested exec by using echo whithin, so exec is enabled – Lu Roman Oct 13 '14 at 02:16
  • @Adelphia i over simplified the problem, i need the main script to execute the service-oauth.php as a background process, to upload a file to youtube asynchronously. But to do all that first i have to check that i can execute a php script like this. If it doesn't work by any means, then i'll try curl or something else. – Lu Roman Oct 13 '14 at 02:21
  • allow_url_fopen=Off isn't set in your php.ini? service-oauth could easily be getting blocked due to that or safe mode... Might be a clue if you tail your php error_log immediately after trying to run the script in a web browser. Running it command line will give different results of course. – Will Oct 13 '14 at 02:28
  • @Dagon I'll try with ssh – Lu Roman Oct 13 '14 at 02:29
  • @quasivivo allow_url_fopen is on, but i'll go check the log – Lu Roman Oct 13 '14 at 02:31
  • @Dagon Calling the script directly from the server console worked, using both the env variable php and the /usr/bin/php path – Lu Roman Oct 13 '14 at 03:40

1 Answers1

0

I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented. – Zagen

Community
  • 1
  • 1
Armali
  • 18,255
  • 14
  • 57
  • 171