20

I want to run a .php file in windows cmd. I followed this (suggestion) php is not recognized as an internal command (in Windows)

But it did not work.

I tried this command prompt:
C:\Windows\system32> cd \myfolder

And within myfolder I did as follows:
C:..\myfolder> php file.php
but I get an error "php is not recognized as internal or external"

But when I try without php command like this;
C:..\myfolder> file.php
it just opens in notepad, yet I want to run it in cmd.
How can I do it?

MwamiTovi
  • 2,425
  • 17
  • 25
user1687804
  • 251
  • 1
  • 2
  • 7

4 Answers4

41

If running Windows 10:

  1. Open the start menu
  2. Type path
  3. Click Edit the system environment variables (usually, it's the top search result) and continue on step 6 below.

If on older Windows:

  1. Show Desktop.

  2. Right Click My Computer shortcut in the desktop.

  3. Click Properties.

  4. You should see a section of control Panel - Control Panel\System and Security\System.

  5. Click Advanced System Settings on the Left menu.

  6. Click Enviornment Variables towards the bottom of the System Properties window.

  7. Select PATH in the user variables list.

  8. Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.

  9. Click OK

  10. Open your "cmd"

  11. Type PATH, press enter

  12. Make sure that you see your PHP folder among the list.

That should work.

Note: Make sure that your PHP folder has the php.exe. It should have the file type CLI. If you do not have the php.exe, go ahead and check the installation guidelines at - http://www.php.net/manual/en/install.windows.manual.php - and download the installation file from there.

Daryn
  • 4,791
  • 4
  • 39
  • 52
janenz00
  • 3,315
  • 5
  • 28
  • 37
  • Thanks i have done all of this. But what about the php.exe file. I havn't it. Is it need to convert php file as .exe ? – user1687804 Oct 13 '12 at 07:51
  • 1
    When you type "php file.php", the first php is actually the php.exe, command line tool. Just edited the answer. Please check – janenz00 Oct 13 '12 at 07:56
  • ok, what i have done. my path is C:\myfolder\file.php I edit variable path with another path like ( .;path;C:\myfolder ) And i typed in cmd C:\Windows\system32>cd \myfolder and get my folder and showing in cmd like C:\myfolder> and then i type C:\myfolder>php file.php then it just showing "php is not recognized as internal or external" what i need to do ? – user1687804 Oct 13 '12 at 08:06
  • 1
    Go to http://windows.php.net/download/ , download an installer first. Then, install PHP in your machine by clicking the installer. Then find out the path in which the PHP was installed. Follow my instructions and add the PHP path to the PATH variable – janenz00 Oct 13 '12 at 08:08
  • I have wamp installed in my pc. is it enough or need to install that ? ( i have already download it . ) – user1687804 Oct 13 '12 at 08:13
  • Wamp is fine - Then, your PHP folder will be somethng like - \wamp\bin\php\php5.3.8 – janenz00 Oct 13 '12 at 08:14
  • ok, i typed : cd C:\wamp\\bin\php\php5.3.5 ( my version id 5.3.5 ) then it just gone to that folder in CMD Like : C:\wamp\\bin\php\php5.3.5 . so, now ? – user1687804 Oct 13 '12 at 08:24
  • php \file.php (This could very well have been another question. LOL – janenz00 Oct 13 '12 at 08:26
  • Nice it's working. Thanks. But it's just showing my php code in CMD. – user1687804 Oct 13 '12 at 08:38
  • Suppose, work of my php script is search a file. but in this method php script showing in cmd console but how can i work here ? – user1687804 Oct 13 '12 at 08:49
  • Run "php -i" without file arguments. That should show you the PHP configurations. If it doesn't, there is some issue with your WAMP installation – janenz00 Oct 13 '12 at 08:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17967/discussion-between-user1687804-and-janenz00) – user1687804 Oct 13 '12 at 09:03
  • 1
    OK, I have just copied that php.exe from wamp folder. And now i can from my directory. Thanks. – user1687804 Oct 13 '12 at 10:47
  • @user1687804: I suggest you to edit your post and notify about your above comment which guide to copy the `php.exe` to another folder , that's very helpful . – URL87 Jun 17 '14 at 07:09
3

you can for example: set your environment variable path with php.exe folder e.g c:\program files\php

create a script file in d:\ with filename as a.php

open cmd: go to d: drive using d: command

type following command

php -f a.php

you will see the output

1

You should declare Environment Variable for PHP in path, so you could use like this:

C:\Path\to\somewhere>php cli.php

You can do it like this

devdRew
  • 4,393
  • 3
  • 24
  • 33
0

It seems your question is very much older. But I just saw it. I searched(not in google) and found My Answer.

So I am writing its solution so that others may get help from it.

Here is my solution.

Unlike the other answers, you don't need to setup environments.

all you need is just to write php index.php if index.php is your file name.

then you will see that, the file compiled and showing it's desired output.