简体   繁体   English

如何在 Windows 上的任何地方运行 .phar?

[英]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希望有帮助

You would need to add the folder containing file.phar to your path variable.您需要将包含file.phar的文件夹添加到您的路径变量中。

See this link for more information.有关更多信息,请参阅此链接

To make wp-cli.phar work,为了使wp-cli.phar工作,

  1. Create wp.bat创建 wp.bat
  2. Put the following in it:将以下内容放入其中:
@ECHO OFF
php %~dpn0.phar %*

%~dpn0: Is a placeholder for $0 (Linux users will understand). %~dpn0:是 $0 的占位符(Linux 用户会理解)。

  1. The directory where you want to place wp.bat , make sure to add that path to the environtment variables.要放置wp.bat的目录,请确保将该路径添加到环境变量中。
  2. Rename 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.batwp.phar 。最好将两者放在同一目录中。
  3. Open a new PowerShell window, and execute打开一个新的 PowerShell 窗口,然后执行

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)

  1. 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).;.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.这是一个以分号分隔的文件扩展名列表,它们将在同一命令窗口中运行,而不是启动它们自己的窗口。
  2. Set the default program for .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文件时打开。
  3. At this point you can run .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一样。
  4. You have to close and re-open any cmd/powershell windows before environment variable changes go into effect.在环境变量更改生效之前,您必须关闭并重新打开任何 cmd/powershell 窗口。
  5. Run your .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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM