Reputation: 135
Hi I want to execute a PHP file under windows CMD with PHP CLI server
on linux i am doing something like that :
on my /usr/local/bin
I have a file called Jimboo
that file contains the following
#!/usr/bin/php
<?php
require "/var/www/jimboo/run/index.php";
so on my terminal i just type Jimboo
and my php application fire sup
so how can i do something similar to this ? how can i use custom global command to run my app on CMD
note : i can do php link to my index.php
but this is not so practical . thanks
Upvotes: 1
Views: 2034
Reputation: 9620
In order to run php globally, make sure you have the php cli
set up properly in the environment variables.
if you wish to make it as simple as entering the name and accessing it, and want to try something a bit different as mentioned in the other answer, do this.
Compile this program in c++ to make an executable Jimboo.exe
and add the location of Jimboo.exe
to your PATH. Then only typing in Jimboo
in cmd will execute the command.
#include<iostream>
using namespace std;
int main()
{
cout << "Running Jimboo";
system("php yourPHPfile.php argument1 argument2 ... ");
}
make sure you add php to PATH before using the above code.
Upvotes: 1
Reputation: 18416
Windows does not support the shebang
syntax like Linux does.
To configure the .php
file extension association to open with the PHP executable.
.php
file (or any file extension you want to associate with php.exe)Open With
More Apps
Choose another app
Look for another app on this PC
php.exe
executable and select itAlways use this app to open .php files
Afterward you can open up a command prompt (cmd
) and have php execute .php
files by path without needing to prefix php.
You can also execute a .php
file by double clicking them (though the PHP terminal will not stay opened after executing unless you add sleep
just like a .bat
file functions).
I used
.php3
as an example since I have.php
configured to open with my IDE.
d:\test.php3
<?php
echo 'Hello World';
To make the .php
file extension an executable like in linux with chmod 0111
. You would have to append ;.PHP
to the PATHEXT
Windows Environment Variable.
To make it the file globally accessible, append the directory the .php
file is located in to your PATH
Windows Environment Variable. Otherwise move the .php
file to a directory already listed in your PATH
Windows Environment Variable, such as C:\Windows
or C:\Windows\System32
To initialize the new environment variable, either launch a new command prompt (cmd
) as Administrator and close it or reboot.
Alternatively create a C:\Windows\Jimboo.bat
file that contains.
@echo off
"X:\path\to\php.exe" "X:\path\to\index.php"
This will then execute the desired file in a command prompt by typing Jimboo
.
Upvotes: 0