How to run .phar from anywhere on Windows? - php

I have .phar file and i want to run it from anywhere with this command.
php file.phar --options
where file.phar is in C:\my-php-cli\file

That is not possible. For example, putting file.phar to C:\Windows\System32 and calling php file.phar --options from another directory like C:\Temp will produce
Could not open input file: file.phar
You have to create a small batch file next to your *.phar, e.g., run-file.bat with this contents:
#ECHO OFF
php %~dp0file.phar %*
Then, calling run-file will execute your file.phar.

For Windows 10 I found that you need a your-file.cmd to run the .phar file.
I was trying to get php-cs-fixer running so in my PHP directory i had the following:
PHP\
phpFixer\
php-cs-fixer.phar
php-cs-fixer.cmd
Contents of php-cs-fixer.cmd:
#php "%~dp0php-cs-fixer.phar" %*
verified it works by entering this in command line:
php-cs-fixer --version
PHP CS Fixer 2.14.0 Sunrise by Fabien Potencier and Dariusz Ruminski (b788ea0)
Hope that helps

You would need to add the folder containing file.phar to your path variable.
See this link for more information.

To make wp-cli.phar work,
Create wp.bat
Put the following in it:
#ECHO OFF
php %~dpn0.phar %*
%~dpn0: Is a placeholder for $0 (Linux users will understand).
The directory where you want to place wp.bat, make sure to add that path to the environtment variables.
Rename wp-cli.phar to wp.phar (should be the same as the name of the .bat file, i.e. wp.bat and wp.phar. It is better to have both in the same directory.
Open a new PowerShell window, and execute
wp --help
The above command will only work if you have PHP installed.

This is how you make .phar files work the way .bat or .vbs files work where you can run them directly or add them to your path and it will automatically run them through the appropriate interpreter (in this case PHP)
Add ;.PHAR to the end of your PATHEXT environment variable (searching "Edit the system environment variables" in the start menu should get you to the right place). This is a semicolon separated list of file extensions that will be run in the same command window rather than launching their own window.
Set the default program for .phar files to be php.exe. You can get to the settings for this by searching "Choose a default app for each type of file" in the start menu or by selecting "Try an app on this PC" > "Look for another app on this PC" in the dialogue that opens the first time you try to open a .phar file.
At this point you can run .phar files just like you would run a .bat file. You don't have to worry about what directory php.exe is in, but you do have to worry about what directory the .phar is in. If you want to make a particular .phar file available from any folder it needs to be inside a folder listed in your PATH. Either move the .phar to an existing folder in your PATH (like C:\Windows\system32) or add the folder containing the .phar to your PATH the same way you modified PATHEXT in step 1.
You have to close and re-open any cmd/powershell windows before environment variable changes go into effect.
Run your .phar file without putting php in front. If it is in your PATH you can optionally omit the .phar at the end as well. This is nice if you are using, for example, composer.phar. Once you set everything up and add it to your path, you can do things like composer --version from any directory.

Related

Is it possible to run a php script without specifying the path to the script?

I need to run a php script from the command line. The problem is, this script will be in a different location on different PCs.
So I want to be able to enter something like this:
php myscript.php
where myscript.php will be in a folder which is in the windows path, but not in the current folder.
Is it possible to do something like this?
Please refer to this link in the php manual.
http://php.net/manual/en/install.windows.commandline.php
On any pc you have to setup the PATH and PATHEXT variable.
Append the location of the PHP executable (php.exe, php-win.exe or
php-cli.exe depending upon your PHP version and display preferences)
to the PATH environment variable. Read more about how to add your
PHP directory to PATH in the corresponding FAQ entry.
Append the .PHP extension to the PATHEXT environment variable. This
can be done at the same time as amending the PATH environment
variable. Follow the same steps as described in the FAQ but amend
the PATHEXT environment variable rather than the PATH environment
variable.
Associate the .PHP extension with a file type. This is done by
running the following command: assoc .php=phpfile
Associate the phpfile file type with the appropriate PHP executable.
This is done by running the following command: ftype phpfile="C:\PHP5\php.exe" -f "%1" -- %~2
This will allow your script to run as normal shell command, like a batch file.
You will run it as :
myscript.php
and not
php myscript.php

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.

How to update PATH Variable permanently Using PHP

Hi there I want to update Path environmental variable permanently through PHP script. I know about putenv() but it is not permanent. I have read some where of generating a .reg file with following content.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
'PATH'='c:/abc/test/ImageMagick-6.7.8-Q8/convert.exe'
I have generated the file with content.
When I run this file, it shows me that file has been run successfully and key values containing in file is added in registry successfully. But When I go to path variable to check, my values are not there :-(
Can any body tell me why is this happening. I am using window 7. But my work has also to be compatible with window xp. Or is there any other way to accomplish this task?
Need suggestions....
Best Regards
The PATH environment variable in the registry is copied to a process' environment when the process is created.
cmd.exe (the command window or shell) is a process in it's own right. You have to SET PATH= within the shell to change it.
If you open a new command window, you should have the new PATH set. See also Programmatically adding a directory to Windows PATH environment variable
Hi I have solved this issue. I have solved it by creating .bat file. The content of .bat file looks like
#echo off
set KeyName=Path
set KeyValue="D:\songs;%PATH%"
setx -m %KeyName% %KeyValue%
-m is used if you want to set it at system level for all users. If you want it to set only for current user, then remove -m. The above command will set D:\songs in environement Path variable. To run this you need to be the administrator of the system.
Cheers if this is useful for any one :-)

Run .php file in Windows Command Prompt (cmd)

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.

How do I run a PHP script using windows schedule task?

I have installed localhost/server in my machine and I need to run a php script using windows schedule task. how do I add path in "Actions" tab in schedule task / cofigure the script to run for particular period?
Locate the php.exe executable on your system and pass it the name of the script file using the -f parameter.
Example:
C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
Reference:
Introduction to using PHP on the command line
PHP command line options
Here's how did it.
Windows scheduler -> create a new task -> action tab -> Edit
At least I tried out some suggestions but it doesn't work so I tried this.
Use a bat file and schedule to execute that bat file.
For example in the bat file executephp.bat, write this
c:\xampp\php\php.exe -f c:\xampp\htdocs\do_something.php
save that bat file that contains that line.
Go to windows scheduler and create a new task and in action tab, browse to point that executephp.bat and for start in -> direct to the directory u have that executephp.bat.
For example if u save the file under C:\xampp\htdocs put that C:\xampp\htdocs in the start in.
Remember to invoke the script even when the user is not logged on.
Everything is set and it will execute without problem.
You can use PHP Command Line to execute it rather then trying to load it through the browser.
Under the actions tab, create a new action and:
Program/Script: Point to your PHP.exe file
Add Arguments: -f /path/to/php/file.php
Optionally you can make it start in the script's directory as well.
create Schedule task
Scheduler->Actions->Edit Action
if you have php file
Program/script: powershell
Add arguments: curl http://localhost/demo/cron.php
if you have MVC/CMS URL
Program/script: powershell
Add arguments: curl http://localhost/demo/controller/method
I just wanted to leave what i had to do to get this working for server 2012. Which was what has previously been said but with added quotes and using the 'Add arguments' box. So in Task Scheduler->Actions->Edit Action.
Program/script: "C:\xampp\php\php.exe"
Add arguments: -f "<full path and filename>"
Start in: <Path to file>
(I had to use 'start in' as i referred to class's within the script)
Hope this helps.
Here's how I did it.
In the Run box: c:\location_of_my_php_installation\php.exe -f c:\location_of_my_php_file\php_file.php
In the Start in box: c:\location_of_my_php_installation\php.exe
you can directly call your local host url by using
explorer "http://localhost/yourFile.php"
If the answer given by Pekka does not work (C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php), make sure that you have the correct PHP extensions enabled as well as the correct php.ini file being used for the PHP version you are using.
I ran into this issue recently and resolved it. I was using PHP v5.4 to run my script.php that was nested within the top level PHP folder (v5.3). When I ran the script.php, from within the v5.4 folder, it was using the v5.3 php.ini file with different extensions, which caused the script.php to fail.
To fix this, here is what I did within the Task Scheduler : Actions tab
Program Script:
"C:\Program Files (x86)\PHP\v5.4\php.exe"
Add Arguements:
-c "C:\Program Files (x86)\PHP\v5.4\php.ini" -f "C:\Xampp\htdocs\script.php"
Using the -c option, you can specify which php.ini file should be used (stackflow answer).
I verified which php was in use by following this stackflow answer.
In command line, type php -m to check which extensions are enabled.
Then, use php --ini to check which .ini file(s) is/are being read by PHP.
You may not be using the correct ini file for the PHP version you are using if you have multiple versions installed.
You dont need a .bat file just set the options in the task scheduler:
So here i've just pointed to the php.exe and then in the arguments the
-f fullpath2PHP file
The important bit is the Start in part - this should be the folder your php file (the one you want to run) sits in.
so for example:
File location
-f c:\inetpub\wwwroot\runthisplease\myFile.php
Start in
c:\inetpub\wwwroot\runthisplease\
You can test this by creating a windows shortcut (but you cannot schedule a shortcut link)
In the properties for your task in the Task Scheduler, change the account for which your script is running under to the SYSTEM account.
Beaware! You should ONLY do this for scripts you fully trust - Doing this elevates the privileges to of the script to beyond administrator.
To create a scheduler in Windows, you need to:
Create .bat file on your server or system;
Type the following command in your.bat file: “F:\xampp\php\php.exe” -f “F:/xampp/htdocs/sitefolder/test.php”;
Set the scheduler time and file in your task scheduler in Windows.

Categories