Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run .php file in Windows Command Prompt (cmd)

Tags:

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?

like image 539
user1687804 Avatar asked Oct 13 '12 07:10

user1687804


People also ask

Can you run PHP from command line?

On Windows, PHP can be configured to run without the need to supply the C:\php\php.exe or the . php extension, as described in Command Line PHP on Microsoft Windows.

How can I run a PHP file?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

Can I run PHP in Windows?

PHP can be installed anywhere on your system, but you'll need to change the paths referenced below if C:\php isn't used.


2 Answers

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.

like image 167
janenz00 Avatar answered Sep 18 '22 05:09

janenz00


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

like image 33
user2458523 Avatar answered Sep 19 '22 05:09

user2458523