Tuesday, May 13, 2008

Setting up registry to run .php scripts from command line

I wanted to have a script in php that I could run in the following way:

c:\tmp> myscript.php argument1 "second argument"

This is what I came up to:

a) In the file explorer (not internet! file!) double click on your .php script, select the 'Select the program from a list', Browse to php.exe, mark 'always use the selected program to open this kind of file', and accept.

This, will create a HKEY_CLASS_ROOT/.php entry with the value php_auto_file.
It will also create the HKEY_CLASS_ROOT/php_auto_file/shell/open/command with the default entry: "c:\php\php.exe" "%1"
(the path to the php.exe file depends on where you have installed php in!)

This kind of registration will allow you now running your scripts from the command line but it WON'T allow you to pass any arguments.

b) That's why you have to the HKEY_CLASS_ROOT/php_auto_file/shell/open/command entry to the following: "c:\php\php.exe" "%1" %*

Here you go. Now you can prepare your myscript.php to look like this, and test the execution:
print_r( $argv ) ;

myscript.php argument1 "second argument"


[0] => c:\tmp\myscript.php
[1] => argument1
[2] => second argument

1 comment:

Emi Jackson said...

Thank you so much for sharing this informative blog. Your technical information is really useful for me. Keep update your blog.


PHP Training in Chennai