Run .php file in Windows Command Prompt (cmd) - php

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?

If running Windows 10:
Open the start menu
Type path
Click Edit the system environment variables (usually, it's the top search result) and continue on step 6 below.
If on older Windows:
Show Desktop.
Right Click My Computer shortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settings on the Left menu.
Click Enviornment Variables towards the bottom of the System Properties window.
Select PATH in the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
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.

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

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

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.

Related

'mysqldump' is not recognized as an internal or external command

currently im trying to make backup and restore mysql database in my laravel project. I am using this laravel package https://github.com/backup-manager/laravel for backup package. i already follow the intructions, but when i am trying to backup my local database through command line (php artisan db:backup) in the last question, i got this message.
Dumping database and uploading...
[BackupManager\ShellProcessing\ShellProcessFailed]
'mysqldump' is not recognized as an internal or external command,
operable program or batch file.
I already googling it, and already put C:\xampp\mysql\bin to my windows env variables paths, but i still having this issue. if anybody know how to fix this, please tell me, i really appreciate it.
Laravel 5.1.x
It is because mysqldump.exe is not found in that location, the right path is given bellow
Open Command Prompt and type this
cd C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin
Press Enter
then type
mysqldump.exe
or,
Directly open this directory "C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin" and Press LEFT SHIFT KEY from keyboard and RIGHT CLICK on the Directory Window.
then type
mysqldump.exe
It will work 100% correctly
If you are using Xamp then:
Open your Xamp folder
Go to \mysql\bin find the mysqldump.exe
Go to your environment variables under System variable click on 'New'.
As 'variable name' put 'mysqldump'(without the quotes) and as 'variable value' put the path to your xamp\mysql\bin\mysqldump.exe this will make 'mysqldump' available as a global system alias.
Reminder: Make sure to restart your command-line interface.
It happened with me and I solved it by adding the following line at the top of the .bat file.
cd "C:\xampp\mysql\bin"
Please add C:\xampp\mysql\bin to this path at your user variables and system variables. If you are using a wamp server, add C:\wamp64\bin\mysql\mysql8.0.27\bin this path at your user and system variables.
Note: MySQL version path will change as per your installation.
On my side, I was using PowerShell and I had the same error message.
I had to go inside the directory of MySql. Mine was located in:
C:\Program Files\MySQL\MySQL Server 5.7\bin
Then I had to make sure that I had mysqldump.exe in the bin folder.
And then run this command:
./mysqldump.exe -u root -p yordbName > C:\MySqlDump\sakila.sql
It is late, but I will answer to this question for other people who have the same issue.
You need to add the mysqldump.exe runtime file path to your Windows environment variable. I am using Wamp, the path look like this "C:\wamp64\bin\mysql\mysqlx.x.xx\bin"
Close your command-line interface and try again, It should work.
I am using Xamp and the following step is working:
Copy the path "C:\xampp\mysql\bin"
Go to your environment variables under System variable click on 'Path' and add "New".
Put the path there then click ok.
Restart your command-line interface.

Double click and run the Php script on mac

I am sorry if this question was answered.
Why can't I run php code directly without using terminal on mac.What I mean when you double click on html file it automatically opens in the browser but not in the case of php.If I try to double click on php it opens with some text-editor.
Any help would be helpful.
Try this (for mac),
Open terminal
cd to folder
Start php server - php -S 127.0.0.1:8000
Open browser and enter - http://localhost:8000/file-name.php
I think you don't understand what PHP is ...
HTML is a markup-language, that can directly be understood by the browser. If the browser opens the file, it can do something with the content.
As PHP is a programming-language, you need a parser. This parser is your PHP executable. This program can understand PHP and does nothing more, than running the code and giving something as result. This result may be an HTML webpage, an image or whatever.
Since you said, you're using a mac, here's a quick introduction on how to set up your personal webserver:
On Mac OSX, PHP and Apache (that's what I use in this example) is already installed and pre-configured. You can just start using it like this:
Go into your system preferences and verify that Web Sharing is enabled.
Open the Finder and go to /Library/WebServer/Documents/localhost. All files that are in there are processed by the local webserver (Apache and PHP, if you want to know that). Place your file in there and open your webserver and call http://localhost/YourFile.php and it will call the file YourFile.php and show you what the output of the script is.
EDIT:
If you are using PHP for scripts, like bash-scripts, see the answer #andreas-baumgart provided.
To run PHP in MAC, one should start the built-in Apache Web server and also enable the PHP already installed.
This can be done with the following steps.
Go to /etc/apache2/httpd.conf and change the permission to sudo chmod 777 httpd.conf
Then open the above file to uncomment the line #LoadModule php5_module libexec/apache2/libphp5.so
To start the apache built-in server, use the command sudo apachectl start in the terminal.
Now .php files can be created and run from the terminal using php -f filename.php and it can also be run on a browser using http://localhost/filename.php
You cannot execute plain PHP scripts as they are no executable programs but source code. As such they contain just the receipt for an interpreter to create executable code. In order to run your PHP script you need to pass it to the PHP interpreter. In your scenario you can archive that by providing a shebang.
To run your script on double click try this:
Make the script executable using chmod +x yourscript.php
Prepending the according Shebang to the files content: #!/usr/bin/env php.
Select a PHP file in Finder, hit CMD-i and change "Open With" to "Terminal.app".
Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.
Solution #1
The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:
#!/usr/bin/php
<?php
include('name-of-php-script.php');
?>
Save it as wrapper.command The name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.
Solution #2
The specific inquiry requires a bit of work.
First make sure that the 1st line of the php script is:
#!/usr/bin/php
This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:
whereis php
Once you've added the Shebang line to the php script you've readied it for automatic execution.
To make it double clickable executeable you have to do the following:
Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...
Switch where it says Enable: from Recommended Applications to All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...
OS X will verify you want it to set Terminal as the default application to open .php files
This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.
Try MAMP
MAMP 4 brings even more opportunities for web developers. We are now supporting MySQL 5.6 and Nginx is now fully integrated. Server starting times have been improved.
Because .php files are not 'executable' per se, instead they are just text files with a PHP extension.
You need to run the php interpreter against the file to execute on it's contents.

Why can't I use 'php artisan' cmd in Laravel path (Win)

I have tried to learn Laravel in Ubuntu and the command "php artisan' worked in the laravel folder path in the terminal.
I just start to do it in Win7, I typed "cd c:\wamp\www\myproject" in cmd.exe to change the path to the laravel folder (artisan file is in this folder).
After that I tried "php artisan" but I got this message "'php' is not recognized as an internal or external command, operable program or batch file."
Did I miss something? The command line showed there is C:\wamp\www\myproject>php artisan and I have double checked the file path is right.
Which step did I do wrong?
The error is occurring because the windows command line doesn't know where to find the php.exe binary.
In Windows 7 click START and then type 'environment' into the start-menu search bar. Choose 'edit environment variables for your account'. In previous version of windows right-click My Computer then click properties. Go to advanced tab in the properties window and click the button on the bottom labelled, "Environment Variables."
Now, find the PATH environment variable and add the path to your PHP binary to it. Paths are delimited with semi-colons.
An example PATH variable may look like:
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;D:\apps\Git\bin
Simply add the path to your PHP binary to the end, don't forget the intervening semi-colon. The path to my PHP binary is D:\work\apps\xampp177\php so my PATH variable will look like this:
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;D:\apps\Git\bin;D:\work\apps\xampp177\php
Notice that the path doesn't contain php.exe. The path is only the folder which contains php.exe.
After the PATH has been saved, close your CLI, reopen it, and you should have no further problems using artisan.

Git bash on windows 7. mysqldump command is not working

I'm using git bash on a windows 7 machine. When I try to use mysqldump I'm getting an error:
"sh.exe": mysqldump: command not found". I am very new to this. Can somebody help me?
You'll need mysqldump on the path that bash is using to find commands. UN*X shells, like bash, and also windows cmd, use a PATH environment variable to manage this.
To see your current path, type this in bash:
$ echo $PATH
If you've installed MySQL, it's typically located at c:\Program Files\MySql, and the support programs, like mysqldump, are in c:\Program Files\MySql\MySQL Server 5.5\bin\. You'll need to have that directory on your path. If MySQL has been installed somewhere else on your machine then you'll need to find the bin directory there and use that path in these instructions.
The best way to do this in windows is by changing the user or system environment variables. To do this, in Windows Explorer:
Right-click Computer and select Properties from the menu.
In the resulting window, choose Advanced System Settings in the sidebar.
In the resulting dialog, click the Advanced tab.
Near the bottom, click the Environment Variables button.
There are 2 sections, user at the top, and system below. Either will work, but let's change the system settings.
Scroll the list until you see the variable named Path in the Variable column.
Double-click this Path row.
In the resulting dialog, in the Variable value field, scroll all the way to the end of the field, and at the end, add this: ;c:\Program Files\MySql\MySQL Server 5.5/bin/. Note the leading semicolon: it won't work without it.
Click OK and now you can close all the dialogs/windows.
Once you've done all that, close the Git bash window, and open another one. Type:
$ mysqldump
You should get Usage instructions from the program.
If not, look at the path again by typing:
$ echo $PATH
Do you see anything like what you added? Any MySQL directories there? It's likely you've just mistyped something, or missed the semicolon. Go over the steps again and make sure everything is correct.

‘php.exe’ is not recognized as an internal or external command, operable program or batch file

‘php.exe’ is not recognized as an internal or external command,
operable program or batch file.
Why does that error occur even though I've added PHP to my environment variables?
My environment variable PATH is shown below:
C:\Program Files\NVIDIA Corporation\PhysX\Common;C:\Program
Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files\PC
...............
Files\QTSystem\;A:\xampp\php\php.exe
I'm using Windows 7.
A:\xampp\php\php.exe
The PATH environment variable must contain paths only, not file names.
Make that
A:\xampp\php
it is very easy to do
You need to also add php.exe
Adding this path:
windows key + pause
click on Advance system settings
click Environment Variable
System variables part -> Path -> Edit and the add
C:\wamp\bin\php\php5.3.8;
close you cmd prompt if its open
It's Done!
I think you need to change it to A:\xampp\php\ and just leave it there, then it will search the directory for the file, also: have you tried calling just php instead of php.exe and check that the directory is correct.
View above screen shot for setup enviroment variable in window 7
Note:- After setup enviroment variable please restart you computer then its will work fine
Shouldn't the last part be: C:\xampp\php\ ? Unless you remapped your drives, A is the floppy disk. :)
Also, you need to specify the folder, not the exe file itself.
My case am Using Bitnami Ruby Stack apache Web server ,
Pressed Windows key + Pause/Break Button
Clicked advanced system settings
Click Environment Variables
Under System variables > edit PATH variable by adding C:\Bitnami\rubystack-1.9.3-24\php
Click Ok to close the Environment Variable window
Close your command prompt if open
*(Proceed to 7. to confirm if php is installed)
Press Windows Key + R button to open your cmd
Type php -v to view your php version
Set PHP as environment variable from System as below to run it from CMD
1)Windows key + Pause
2)Click on Advanced system settings
3)Now click on Environment Variables.. button
4)In system variable section search for Path and edit it as D:\wamp\bin\php\php5.3.13;(If one needs to append it to some already exits value then start path with;)
5)Now open CMD and run command PHP from any path where you don’t have PHP installed , in my case it’s C drive.
6)We can check above set Path from CMD using Path command also.
If you have installed WAMP server, then your path can vary. For example, I installed WAMP and the path to my php.exe turned out to be:
C:\wamp\bin\php\php5.5.12\php.exe
To make it work, I entered the following path in the PATH variable of the System Variables:
C:\wamp\bin\php\php5.5.12
Once done, I restarted command prompt and everything worked fine.
So the path can vary depending upon how you installed PHP. If you would like to install WAMP server and have it install other essential packages like PHPMyAdmin, etc. you may do so from: wampserver.com
In my case, environment path is C:\ProgramData\Drush\;C:\Program Files (x86)\Drush\Php; and it works for me.
If I simply add C:\xampp\php\ dint work for me.
I hope if someone facing this issue can try this suggestion.
This happened to me and I found why.
Xampp can't run properly depending on your UAC settings.
Try to do this:
Log in as administrator (running cmd.exe as administrator isn't enough)
Execute cmd.exe or equivalent
type php -version

Categories