2

I'm trying to create a script that runs from the webserver that will run a php file via exec(), but I want it to do so without prepending the php to it. Can this be done?

Using Windows 2008 and XAMPP

Rob
  • 2,303
  • 9
  • 31
  • 50

2 Answers2

5

No, a php file cannot be directly executable in Windows. You would need a workaround like wrapping the php filename.php command in a batch file.

Shane Madden
  • 112,982
  • 12
  • 174
  • 248
1

It can be done in Windows 7 - I don't know if the same idea applies to Windows 2008 - however, the effects of this approach may outweigh the benefits.

Essentially, all you need to do is make php-cli.exe the default program to open .php files.

I did this in two ways, I am not sure if both ways are required.

Firstly, make php-cli.exe (or php.exe I suppose) the default program for opening .php files.

In Windows that equates to: right click a .php file, select 'Open With...', browse for and select the php-cli.exe executable, and 'set as default'.

I believe that in Windows 2008, this functionality can be reached via Control Panel > Default Programs > Associate a file type or protocol with a program

Once the php-cli.exe executable is set as the default application for .php files you should have achieved your objective, with a few side effects.

At this point, if you run scriptname.php from command prompt, it will be executed by php-cli.exe, and the output displayed below the prompt - which may be what you want; however, if you double click the file, it will also be executed by php-cli.exe (instead of your preferred editor) - which is probably something you don't want.

However, assoc .php (at least in my case), returned the filetype as corresponding to the editor I use (and checking this filetype with ftype gave the path to the same editor).

To fix this, run something similar to the following (as administrator):

ASSOC .php=PHPScript && FTYPE PHPScript="C:\Program Files (x86)\Xampp\php\php.exe" %1 %* 

After this, accessing my test PHP file in the browser, executed the second test script (without using the php prefix) and displayed the result.

For interest sake, my test scripts were: test2.php (run in browser):

<?php
echo exec('test1.php');
?>

test1.php (in same folder):

<?php
echo date('r');
?>

(Tested on Windows 7 with XAMPP 1.7.7/PHP 5.3.8)

cyberx86
  • 20,620
  • 1
  • 60
  • 80