[英]How to run .phar from anywhere on Windows?
I have .phar file and i want to run it from anywhere with this command.我有 .phar 文件,我想用这个命令从任何地方运行它。
php file.phar --options php 文件.phar --options
where file.phar is in C:\my-php-cli\file其中 file.phar 在 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例如,将file.phar
放入C:\Windows\System32
并从另一个目录(如C:\Temp
)调用php file.phar --options
将产生
Could not open input file: file.phar
You have to create a small batch file next to your *.phar, eg, run-file.bat
with this contents:您必须在 *.phar 旁边创建一个小批处理文件,例如,带有以下内容run-file.bat
:
@ECHO OFF
php %~dp0file.phar %*
Then, calling run-file
will execute your file.phar
.然后,调用run-file
将执行您的file.phar
。
For Windows 10 I found that you need a your-file.cmd to run the .phar file.对于 Windows 10,我发现您需要 your-file.cmd 来运行 .phar 文件。
I was trying to get php-cs-fixer running so in my PHP directory i had the following:我试图让 php-cs-fixer 运行,所以在我的 PHP 目录中我有以下内容:
PHP\
phpFixer\
php-cs-fixer.phar
php-cs-fixer.cmd
Contents of php-cs-fixer.cmd: 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希望有帮助
To make wp-cli.phar
work,为了使wp-cli.phar
工作,
@ECHO OFF
php %~dpn0.phar %*
%~dpn0: Is a placeholder for $0 (Linux users will understand). %~dpn0:是 $0 的占位符(Linux 用户会理解)。
wp.bat
, make sure to add that path to the environtment variables.要放置wp.bat
的目录,请确保将该路径添加到环境变量中。wp-cli.phar
to wp.phar
(should be the same as the name of the .bat
file, ie wp.bat
and wp.phar
. It is better to have both in the same directory.将wp-cli.phar
重命名为wp.phar
(应与.bat
文件的名称相同,即wp.bat
和wp.phar
。最好将两者放在同一目录中。wp --help
The above command will only work if you have PHP installed.上述命令仅在您安装了 PHP 时才有效。
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)这就是使.phar
文件像.bat
或.vbs
文件一样工作的方式,您可以直接运行它们或将它们添加到路径中,它将通过适当的解释器自动运行它们(在本例中为 PHP)
;.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).将;.PHAR
添加到PATHEXT
环境变量的末尾(在开始菜单中搜索“编辑系统环境变量”应该可以让您找到正确的位置)。 This is a semicolon separated list of file extensions that will be run in the same command window rather than launching their own window.这是一个以分号分隔的文件扩展名列表,它们将在同一命令窗口中运行,而不是启动它们自己的窗口。.phar
files to be php.exe
.将.phar
文件的默认程序设置为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.您可以通过在开始菜单中搜索“为每种类型的文件选择默认应用程序”或在对话框中选择“在此 PC 上试用一个应用程序”>“在此 PC 上查找另一个应用程序”来进行此设置在您第一次尝试打开.phar
文件时打开。.phar
files just like you would run a .bat
file.此时,您可以像运行.bat
文件一样运行.phar
文件。 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
.您不必担心php.exe
所在的目录,但您必须担心.phar
所在的目录。如果您想让特定的.phar
文件可从任何文件夹中使用,则该文件需要在其中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.将.phar
移动到PATH
中的现有文件夹(如C:\Windows\system32
)或将包含.phar
的文件夹添加到PATH
中,就像在步骤 1 中修改PATHEXT
一样。.phar
file without putting php
in front.运行您的.phar
文件,而不将php
放在前面。 If it is in your PATH
you can optionally omit the .phar
at the end as well.如果它在您的PATH
中,您也可以选择在末尾省略.phar
。 This is nice if you are using, for example, composer.phar
.例如,如果您使用composer.phar
,这很好。 Once you set everything up and add it to your path, you can do things like composer --version
from any directory.设置好所有内容并将其添加到路径后,您可以从任何目录执行composer --version
之类的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.