1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
archived-doc-zh/install/windows/commandline.xml
mowangjuanzi 2fe37ec96a Update install
2024-10-16 22:27:17 +08:00

194 lines
5.7 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 2dbf3d9064d4cb07f0a2f7d06641c877a2e5ed24 Maintainer: Luffy Status: ready -->
<sect1 xml:id="install.windows.commandline" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>在 Windows 系统的命令行上运行 PHP</title>
<para>
本章包含有针对在 Windows 下以命令行运行 PHP 的说明与提示。
</para>
<note>
<para>
阅读 <link linkend="install.windows.manual"> 手工安装步骤</link>
</para>
</note>
<para>
要在命令行下运行 PHP可以无需对 Windows 做任何改动。
<screen>
<![CDATA[
C:\php\php.exe -f "C:\PHP Scripts\script.php" -- -arg1 -arg2 -arg3
]]>
</screen>
</para>
<para>
但是有几个很容易的步骤可以使其更加简便。某些步骤可能已经在之前完成了,不过还是在这里重复说明以便提供一个完整的步骤序列。
<itemizedlist>
<note>
<para>
<envar>PATH</envar><envar>PATHEXT</envar> 都是在 Windows
下已有的重要环境变量,要留意不要覆盖了其内容,仅仅是向其中添加内容。
</para>
</note>
<listitem>
<para>
将 PHP 可执行文件(<filename>php.exe</filename><filename>php-win.exe</filename>
或者 <filename>php-cli.exe</filename>)的路径添加到 <envar>PATH</envar>
环境变量中去。如何将 PHP 目录添加到 <envar>PATH</envar>
中请参阅<link linkend="faq.installation.addtopath">与之相关的常见问题</link>
</para>
</listitem>
<listitem>
<para>
<literal>.PHP</literal> 后缀添加到
<varname>PATHEXT</varname> 环境变量中去。可以在修改
<envar>PATH</envar> 环境变量时同时进行。跟<link
linkend="faq.installation.addtopath">常见问题</link>中说明的步骤一样,要修改的是
<varname>PATHEXT</varname> 环境变量而不是
<envar>PATH</envar> 环境变量。
<note>
<para>
<literal>.PHP</literal> 放置到什么位置将决定具有相同文件名时运行的优先级。例如将
<literal>.PHP</literal> 放到 <literal>.BAT</literal>
之前将导致如果有同名的 PHP 脚本和批处理文件,则 PHP 脚本会运行。
</para>
</note>
</para>
</listitem>
<listitem>
<para>
<literal>.PHP</literal> 后缀关联为一种文件类型,用以下命令完成:
<screen>
<![CDATA[
assoc .php=phpfile
]]>
</screen>
</para>
</listitem>
<listitem>
<para>
<literal>phpfile</literal> 文件类型关联到适当的 PHP
可执行文件,用以下命令完成:
<screen>
<![CDATA[
ftype phpfile="C:\php\php.exe" -f "%1" -- %~2
]]>
</screen>
</para>
</listitem>
</itemizedlist>
</para>
<para>
按照以上步骤将使 PHP 脚本可以在任何目录下运行,不需要输入 PHP 可执行文件名以及
<literal>.PHP</literal> 后缀,并且所有参数都会被传递给脚本来处理。
</para>
<para>
以下例子说明了可以手工修改的注册表项目变化。
<example>
<title>注册表变化</title>
<screen>
<![CDATA[
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.php]
@="phpfile"
"Content Type"="application/php"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile]
@="PHP Script"
"EditFlags"=dword:00000000
"BrowserFlags"=dword:00000008
"AlwaysShowExt"=""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\DefaultIcon]
@="C:\\php\\php-win.exe,0"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell]
@="Open"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell\Open]
@="&Open"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell\Open\command]
@="\"C:\\php\\php.exe\" -f \"%1\" -- %~2"
]]>
</screen>
</example>
</para>
<para>
有了这些改变之后,本页顶端第一个例子中的命令可以写成这样:
<screen>
<![CDATA[
"C:\PHP Scripts\script" -arg1 -arg2 -arg3
]]>
</screen>
或者如果 <literal>"C:\PHP Scripts"</literal> 目录位于
<envar>PATH</envar> 环境变量中的话:
<screen>
<![CDATA[
script -arg1 -arg2 -arg3
]]>
</screen>
</para>
<note>
<para>
当打算使用这种技术作为命令行过滤器运行 PHP 脚本时,会出现一个小问题,例如以下例子:
<screen>
<![CDATA[
dir | "C:\PHP Scripts\script" -arg1 -arg2 -arg3
]]>
</screen>
或者
<screen>
<![CDATA[
dir | script -arg1 -arg2 -arg3
]]>
</screen>
脚本可能会直接挂起,没有任何输出。
要使其正常运行,需要对注册表进行另一项更改:
<screen>
<![CDATA[
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer]
"InheritConsoleHandles"=dword:00000001
]]>
</screen>
有关此问题的更多信息见<link
xlink:href="http://support.microsoft.com/default.aspx?scid=kb;en-us;321788">微软知识库文章321788</link>
从 Windows 10 开始,此设置似乎被颠倒,请参考以下链接 <link
xlink:href="https://social.msdn.microsoft.com/Forums/en-US/f19d740d-21c8-4dc2-a9ab-d5c0527e932b/nasty-file-association-regression-bug-in-windows-10-console?forum=windowssdk">
Microsoft 论坛帖子</link>
</para>
</note>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->