jimy razit
jimy razit

Reputation: 135

How to execute PHP globally on Windows CMD

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

Answers (2)

Yash Kumar Verma
Yash Kumar Verma

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

Will B.
Will B.

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.

  1. right click a .php file (or any file extension you want to associate with php.exe)
  2. click Open With
  3. Select More Apps
  4. scroll to the bottom Choose another app
  5. Select Look for another app on this PC
  6. Navigate to the php.exe executable and select it
  7. Check the Always use this app to open .php files
  8. click Ok

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.

PATHEXT

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.

cmd php


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.

Batch File PHP

Upvotes: 0

Related Questions