<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/refs.compression.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'book.phar.php',
    1 => 'Phar',
    2 => 'Phar',
  ),
  'up' => 
  array (
    0 => 'refs.compression.php',
    1 => '压缩与归档扩展',
  ),
  'prev' => 
  array (
    0 => 'function.lzf-optimized-for.php',
    1 => 'lzf_optimized_for',
  ),
  'next' => 
  array (
    0 => 'phar.setup.php',
    1 => '安装/配置',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/phar/book.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/book.phar.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="book.phar" class="book">
 
 <h1 class="title">Phar</h1>
 
 
 <div id="intro.phar" class="preface">
  <h1 class="title">简介</h1>
  <p class="para">
   phar 扩展提供了一种将整个 PHP 应用程序放入单个叫做“phar”（PHP 归档）文件的方法，以便于分发和安装。 
   除了提供此服务外，phar 扩展还提供了一种文件格式抽象方法，用于通过 <span class="classname"><a href="class.phardata.php" class="classname">PharData</a></span> 类创建和操作
   tar 和 zip 文件，就像 PDO 提供访问不同数据库的统一接口一样。与不能在不同数据库之间转换的 PDO 不同，phar
   还可以使用一行代码在 tar、zip 和 phar 文件格式之间进行转换。参见 <span class="function"><a href="phar.converttoexecutable.php" class="function">Phar::convertToExecutable()</a></span> 中的示例。
  </p>
  <p class="para">
   什么是 phar？phar 归档的最佳特征是可以将多个文件组合成一个文件。 因此，phar 归档提供了在单个文件中分发完整的 PHP
   应用程序并无需将其解压缩到磁盘而直接运行文件的方法。此外，phar 归档可以像任何其他文件一样由 PHP 在命令行和 Web
   服务器上执行。phar 有点像 PHP 应用程序的移动存储器。
  </p>
  <p class="para">
   phar 通过 <a href="book.stream.php" class="link">stream 封装协议</a>实现这个功能。通常，要在 PHP 脚本中使用外部文件，将使用 <span class="function"><a href="function.include.php" class="function">include</a></span>：
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 使用外部文件</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /> </span><span style="color: #007700">include </span><span style="color: #DD0000">'/path/to/external/file.php'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   PHP 实际上将 <code class="literal">/path/to/external/file.php</code> 转换为 stream
   封装协议<code class="literal">file:///path/to/external/file.php</code>，在底层，实际上使用普通文件 stream
   封装协议 stream 函数访问所有的本地文件。
  </p>
  <p class="para">
   要使用 phar 归档 <code class="literal">/path/to/myphar.phar</code> 中名为 <code class="literal">file.php</code>
   的文件，语法跟上面的 <code class="literal">file://</code> 语法非常相似。
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>示例 #2 使用 phar 归档内部文件</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /> </span><span style="color: #007700">include </span><span style="color: #DD0000">'phar:///path/to/myphar.phar/file.php'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   实际上可以将 phar 归档完全视为外部磁盘，可以使用任何 <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> 相关函数，<span class="function"><a href="function.opendir.php" class="function">opendir()</a></span>
   和 <span class="function"><a href="function.mkdir.php" class="function">mkdir()</a></span> 相关函数在 phar 归档内读取、更改、创建新文件或目录。这将允许完整 PHP
   应用程序分布在单个文件中，并直接从该文件中运行。
  </p>
  <p class="para">
   phar 归档最常见的用法是在单个文件中分发完整的应用程序。例如，PEAR 安装程序与 PHP 版本捆绑作为 phar
   归档分发。要使用以这种方式分发 phar 归档，可以在命令行或通过 Web 服务器执行归档操作。
  </p>
  <p class="para">
   phar 归档可以作为 <code class="literal">tar</code> 归档、<code class="literal">zip</code> 归档或专门为 phar 扩展设计的自定义
   <code class="literal">phar</code> 文件格式分发。每种文件格式都有优点和缺点。tar 和 zip 文件格式可以由任何可以读取格式的第三方工具读取或提取，但需要
   PHP 和 phar 扩展才能运行。phar 文件格式是针对 phar 扩展定制和独有的，只能由 phar 扩展或 PEAR 包 <a href="https://pear.php.net/package/PHP_Archive" class="link external">&raquo;&nbsp;PHP_Archive</a> 创建，优点是以这种格式创建的应用程序即使未启用 phar 扩展也能运行。
  </p>
  <p class="para">
   换句话说，即使禁用了 phar 扩展，也可以执行或包含基于 phar 的归档。只有使用 phar 扩展才能访问 phar 归档中的单个文件，除非 phar 归档是由 PHP_Archive 创建的。
  </p>
  <p class="para">
   phar 扩展还能够在单个命令中将 phar 归档从 tar 转换为 zip 或 phar 文件格式：
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>示例 #3 将 phar 归档从 phar 转换为 tar 文件格式</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /> $phar </span><span style="color: #007700">= new </span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'myphar.phar'</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$pgz </span><span style="color: #007700">= </span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">convertToExecutable</span><span style="color: #007700">(</span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">TAR</span><span style="color: #007700">, </span><span style="color: #0000BB">Phar</span><span style="color: #007700">::</span><span style="color: #0000BB">GZ</span><span style="color: #007700">); </span><span style="color: #FF8000">// makes myphar.phar.tar.gz<br /> </span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   phar 可以使用 <a href="book.zlib.php" class="link">gzip</a> 或 <a href="book.bzip2.php" class="link">bzip2</a>
   来压缩单个文件或整个归档，并且可以通过使用 MD5、SHA-1、SHA-256 或 SHA-512 签名自动验证归档完整性。
  </p>
  <p class="para">
   最后，phar 扩展有安全意识，默认禁用对可执行 phar 归档的写访问，并且需要系统级禁用 <code class="literal">phar.readonly</code>
   php.ini 设置才能创建或修改 phar 归档。没有可执行存根（executable stub）的普通 tar 和 zip 归档始终可以使用
   <span class="classname"><a href="class.phardata.php" class="classname">PharData</a></span> 类创建或修改。
  </p>
  <p class="para">
   如果正在创建用于分发的应用程序，需要阅读<a href="phar.creating.php" class="link">如何创建 phar 归档</a>。如果想了解关于 phar
   支持的三种文件格式之间差异的更多信息，应该阅读 <a href="phar.fileformat.php" class="link">Phar、Tar 和 Zip</a>。
  </p>
  <p class="para">
   如果使用的是 phar 应用程序，那么在<a href="phar.using.php" class="link">如何使用 phar 归档</a>中有一些有用的提示。
  </p>
  <p class="para">
   <code class="literal">phar</code> 是 <code class="literal">PHP</code> 和 <code class="literal">Archive</code>
   的合成词，大致上基于 Java 开发人员熟悉的 <code class="literal">jar</code>（Java 归档）。
  </p>
  <p class="para">
   phar 归档的实现基于 PEAR 包 <a href="https://pear.php.net/package/PHP_Archive" class="link external">&raquo;&nbsp;PHP_Archive</a>，尽管实现细节相似，但是
   phar 扩展功能更强大。此外，phar 扩展允许大多数 PHP 应用程序不加修改地运行，而基于 PHP_Archive 的 phar 归档通常需要大量修改才能工作。
  </p>
 </div>
 
 
 







 




 





 





 





 




 




 




 





<ul class="chunklist chunklist_book"><li><a href="phar.setup.php">安装/配置</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="phar.requirements.php">需求</a></li><li><a href="phar.installation.php">安装</a></li><li><a href="phar.configuration.php">运行时配置</a></li><li><a href="phar.resources.php">资源类型</a></li></ul></li><li><a href="phar.constants.php">预定义常量</a></li><li><a href="phar.using.php">Using Phar Archives</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="phar.using.intro.php">Using Phar Archives: Introduction</a></li><li><a href="phar.using.stream.php">Using Phar Archives: the phar stream wrapper</a></li><li><a href="phar.using.object.php">Using Phar Archives: the Phar and PharData class</a></li></ul></li><li><a href="phar.creating.php">Creating Phar Archives</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="phar.creating.intro.php">Creating Phar Archives: Introduction</a></li></ul></li><li><a href="phar.fileformat.php">What makes a phar a phar and not a tar or a zip?</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="phar.fileformat.ingredients.php">Ingredients of all Phar archives, independent of file format</a></li><li><a href="phar.fileformat.stub.php">Phar file stub</a></li><li><a href="phar.fileformat.comparison.php">Head-to-head comparison of Phar, Tar and Zip</a></li><li><a href="phar.fileformat.tar.php">Tar-based phars</a></li><li><a href="phar.fileformat.zip.php">Zip-based phars</a></li><li><a href="phar.fileformat.phar.php">Phar File Format</a></li><li><a href="phar.fileformat.flags.php">Global Phar bitmapped flags</a></li><li><a href="phar.fileformat.manifestfile.php">Phar manifest file entry definition</a></li><li><a href="phar.fileformat.signature.php">Phar Signature format</a></li></ul></li><li><a href="class.phar.php">Phar</a> — The Phar class<ul class="chunklist chunklist_book chunklist_children"><li><a href="phar.addemptydir.php">Phar::addEmptyDir</a> — 添加一个空目录到 phar 档案</li><li><a href="phar.addfile.php">Phar::addFile</a> — 将一个文件从文件系统添加到 phar 档案中</li><li><a href="phar.addfromstring.php">Phar::addFromString</a> — 以字符串的形式添加一个文件到 phar 档案</li><li><a href="phar.apiversion.php">Phar::apiVersion</a> — Returns the api version</li><li><a href="phar.buildfromdirectory.php">Phar::buildFromDirectory</a> — Construct a phar archive from the files within a directory</li><li><a href="phar.buildfromiterator.php">Phar::buildFromIterator</a> — Construct a phar archive from an iterator</li><li><a href="phar.cancompress.php">Phar::canCompress</a> — Returns whether phar extension supports compression using either zlib or bzip2</li><li><a href="phar.canwrite.php">Phar::canWrite</a> — Returns whether phar extension supports writing and creating phars</li><li><a href="phar.compress.php">Phar::compress</a> — Compresses the entire Phar archive using Gzip or Bzip2 compression</li><li><a href="phar.compressfiles.php">Phar::compressFiles</a> — Compresses all files in the current Phar archive</li><li><a href="phar.construct.php">Phar::__construct</a> — Construct a Phar archive object</li><li><a href="phar.converttodata.php">Phar::convertToData</a> — Convert a phar archive to a non-executable tar or zip file</li><li><a href="phar.converttoexecutable.php">Phar::convertToExecutable</a> — Convert a phar archive to another executable phar archive file format</li><li><a href="phar.copy.php">Phar::copy</a> — Copy a file internal to the phar archive to another new file within the phar</li><li><a href="phar.count.php">Phar::count</a> — Returns the number of entries (files) in the Phar archive</li><li><a href="phar.createdefaultstub.php">Phar::createDefaultStub</a> — Create a phar-file format specific stub</li><li><a href="phar.decompress.php">Phar::decompress</a> — Decompresses the entire Phar archive</li><li><a href="phar.decompressfiles.php">Phar::decompressFiles</a> — Decompresses all files in the current Phar archive</li><li><a href="phar.delmetadata.php">Phar::delMetadata</a> — Deletes the global metadata of the phar</li><li><a href="phar.delete.php">Phar::delete</a> — 删除 phar 档案中的一个文件</li><li><a href="phar.destruct.php">Phar::__destruct</a> — Destructs a Phar archive object</li><li><a href="phar.extractto.php">Phar::extractTo</a> — Extract the contents of a phar archive to a directory</li><li><a href="phar.getalias.php">Phar::getAlias</a> — Get the alias for Phar</li><li><a href="phar.getmetadata.php">Phar::getMetadata</a> — Returns phar archive meta-data</li><li><a href="phar.getmodified.php">Phar::getModified</a> — Return whether phar was modified</li><li><a href="phar.getpath.php">Phar::getPath</a> — Get the real path to the Phar archive on disk</li><li><a href="phar.getsignature.php">Phar::getSignature</a> — Return MD5/SHA1/SHA256/SHA512/OpenSSL signature of a Phar archive</li><li><a href="phar.getstub.php">Phar::getStub</a> — Return the PHP loader or bootstrap stub of a Phar archive</li><li><a href="phar.getsupportedcompression.php">Phar::getSupportedCompression</a> — Return array of supported compression algorithms</li><li><a href="phar.getsupportedsignatures.php">Phar::getSupportedSignatures</a> — Return array of supported signature types</li><li><a href="phar.getversion.php">Phar::getVersion</a> — Return version info of Phar archive</li><li><a href="phar.hasmetadata.php">Phar::hasMetadata</a> — Returns whether phar has global meta-data</li><li><a href="phar.interceptfilefuncs.php">Phar::interceptFileFuncs</a> — Instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions</li><li><a href="phar.isbuffering.php">Phar::isBuffering</a> — Used to determine whether Phar write operations are being buffered, or are flushing directly to disk</li><li><a href="phar.iscompressed.php">Phar::isCompressed</a> — Returns Phar::GZ or PHAR::BZ2 if the entire phar archive is compressed (.tar.gz/tar.bz and so on)</li><li><a href="phar.isfileformat.php">Phar::isFileFormat</a> — Returns true if the phar archive is based on the tar/phar/zip file format depending on the parameter</li><li><a href="phar.isvalidpharfilename.php">Phar::isValidPharFilename</a> — Returns whether the given filename is a valid phar filename</li><li><a href="phar.iswritable.php">Phar::isWritable</a> — Returns true if the phar archive can be modified</li><li><a href="phar.loadphar.php">Phar::loadPhar</a> — Loads any phar archive with an alias</li><li><a href="phar.mapphar.php">Phar::mapPhar</a> — Reads the currently executed file (a phar) and registers its manifest</li><li><a href="phar.mount.php">Phar::mount</a> — Mount an external path or file to a virtual location within the phar archive</li><li><a href="phar.mungserver.php">Phar::mungServer</a> — Defines a list of up to 4 $_SERVER variables that should be modified for execution</li><li><a href="phar.offsetexists.php">Phar::offsetExists</a> — Determines whether a file exists in the phar</li><li><a href="phar.offsetget.php">Phar::offsetGet</a> — Gets a PharFileInfo object for a specific file</li><li><a href="phar.offsetset.php">Phar::offsetSet</a> — Set the contents of an internal file to those of an external file</li><li><a href="phar.offsetunset.php">Phar::offsetUnset</a> — Remove a file from a phar</li><li><a href="phar.running.php">Phar::running</a> — Returns the full path on disk or full phar URL to the currently executing Phar archive</li><li><a href="phar.setalias.php">Phar::setAlias</a> — Set the alias for the Phar archive</li><li><a href="phar.setdefaultstub.php">Phar::setDefaultStub</a> — Used to set the PHP loader or bootstrap stub of a Phar archive to the default loader</li><li><a href="phar.setmetadata.php">Phar::setMetadata</a> — Sets phar archive meta-data</li><li><a href="phar.setsignaturealgorithm.php">Phar::setSignatureAlgorithm</a> — Set the signature algorithm for a phar and apply it</li><li><a href="phar.setstub.php">Phar::setStub</a> — Used to set the PHP loader or bootstrap stub of a Phar archive</li><li><a href="phar.startbuffering.php">Phar::startBuffering</a> — Start buffering Phar write operations, do not modify the Phar object on disk</li><li><a href="phar.stopbuffering.php">Phar::stopBuffering</a> — Stop buffering write requests to the Phar archive, and save changes to disk</li><li><a href="phar.unlinkarchive.php">Phar::unlinkArchive</a> — Completely remove a phar archive from disk and from memory</li><li><a href="phar.webphar.php">Phar::webPhar</a> — Routes a request from a web browser to an internal file within the phar archive</li></ul></li><li><a href="class.phardata.php">PharData</a> — The PharData class<ul class="chunklist chunklist_book chunklist_children"><li><a href="phardata.addemptydir.php">PharData::addEmptyDir</a> — Add an empty directory to the tar/zip archive</li><li><a href="phardata.addfile.php">PharData::addFile</a> — Add a file from the filesystem to the tar/zip archive</li><li><a href="phardata.addfromstring.php">PharData::addFromString</a> — Add a file from a string to the tar/zip archive</li><li><a href="phardata.buildfromdirectory.php">PharData::buildFromDirectory</a> — Construct a tar/zip archive from the files within a directory</li><li><a href="phardata.buildfromiterator.php">PharData::buildFromIterator</a> — Construct a tar or zip archive from an iterator</li><li><a href="phardata.compress.php">PharData::compress</a> — Compresses the entire tar/zip archive using Gzip or Bzip2 compression</li><li><a href="phardata.compressfiles.php">PharData::compressFiles</a> — Compresses all files in the current tar/zip archive</li><li><a href="phardata.construct.php">PharData::__construct</a> — Construct a non-executable tar or zip archive object</li><li><a href="phardata.converttodata.php">PharData::convertToData</a> — Convert a phar archive to a non-executable tar or zip file</li><li><a href="phardata.converttoexecutable.php">PharData::convertToExecutable</a> — Convert a non-executable tar/zip archive to an executable phar archive</li><li><a href="phardata.copy.php">PharData::copy</a> — Copy a file internal to the tar/zip archive to another new file within the same archive</li><li><a href="phardata.decompress.php">PharData::decompress</a> — Decompresses the entire Phar archive</li><li><a href="phardata.decompressfiles.php">PharData::decompressFiles</a> — Decompresses all files in the current zip archive</li><li><a href="phardata.delmetadata.php">PharData::delMetadata</a> — Deletes the global metadata of a zip archive</li><li><a href="phardata.delete.php">PharData::delete</a> — Delete a file within a tar/zip archive</li><li><a href="phardata.destruct.php">PharData::__destruct</a> — Destructs a non-executable tar or zip archive object</li><li><a href="phardata.extractto.php">PharData::extractTo</a> — Extract the contents of a tar/zip archive to a directory</li><li><a href="phardata.iswritable.php">PharData::isWritable</a> — Returns true if the tar/zip archive can be modified</li><li><a href="phardata.offsetset.php">PharData::offsetSet</a> — Set the contents of a file within the tar/zip to those of an external file or string</li><li><a href="phardata.offsetunset.php">PharData::offsetUnset</a> — Remove a file from a tar/zip archive</li><li><a href="phardata.setalias.php">PharData::setAlias</a> — Dummy function (Phar::setAlias is not valid for PharData)</li><li><a href="phardata.setdefaultstub.php">PharData::setDefaultStub</a> — Dummy function (Phar::setDefaultStub is not valid for PharData)</li><li><a href="phardata.setmetadata.php">PharData::setMetadata</a> — Sets phar archive meta-data</li><li><a href="phardata.setsignaturealgorithm.php">PharData::setSignatureAlgorithm</a> — Set the signature algorithm for a phar and apply it</li><li><a href="phardata.setstub.php">PharData::setStub</a> — Dummy function (Phar::setStub is not valid for PharData)</li></ul></li><li><a href="class.pharfileinfo.php">PharFileInfo</a> — The PharFileInfo class<ul class="chunklist chunklist_book chunklist_children"><li><a href="pharfileinfo.chmod.php">PharFileInfo::chmod</a> — Sets file-specific permission bits</li><li><a href="pharfileinfo.compress.php">PharFileInfo::compress</a> — Compresses the current Phar entry with either zlib or bzip2 compression</li><li><a href="pharfileinfo.construct.php">PharFileInfo::__construct</a> — Construct a Phar entry object</li><li><a href="pharfileinfo.decompress.php">PharFileInfo::decompress</a> — Decompresses the current Phar entry within the phar</li><li><a href="pharfileinfo.delmetadata.php">PharFileInfo::delMetadata</a> — Deletes the metadata of the entry</li><li><a href="pharfileinfo.destruct.php">PharFileInfo::__destruct</a> — Destructs a Phar entry object</li><li><a href="pharfileinfo.getcrc32.php">PharFileInfo::getCRC32</a> — Returns CRC32 code or throws an exception if CRC has not been verified</li><li><a href="pharfileinfo.getcompressedsize.php">PharFileInfo::getCompressedSize</a> — Returns the actual size of the file (with compression) inside the Phar archive</li><li><a href="pharfileinfo.getcontent.php">PharFileInfo::getContent</a> — Get the complete file contents of the entry</li><li><a href="pharfileinfo.getmetadata.php">PharFileInfo::getMetadata</a> — Returns file-specific meta-data saved with a file</li><li><a href="pharfileinfo.getpharflags.php">PharFileInfo::getPharFlags</a> — Returns the Phar file entry flags</li><li><a href="pharfileinfo.hasmetadata.php">PharFileInfo::hasMetadata</a> — Returns the metadata of the entry</li><li><a href="pharfileinfo.iscrcchecked.php">PharFileInfo::isCRCChecked</a> — Returns whether file entry has had its CRC verified</li><li><a href="pharfileinfo.iscompressed.php">PharFileInfo::isCompressed</a> — Returns whether the entry is compressed</li><li><a href="pharfileinfo.setmetadata.php">PharFileInfo::setMetadata</a> — Sets file-specific meta-data saved with a file</li></ul></li><li><a href="class.pharexception.php">PharException</a> — The PharException class</li></ul></div><?php manual_footer($setup); ?>