• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册

    php 文件包Phar

    Phar是英文(“Php ARchive”缩写。是PHP里一个应用,包括所有的可执行、可访问的文件,都打包进了一个文件。Phar可以将一组PHP文件进行打包,还可以创建默认执行的stub(或者叫做 bootstrap loader),Phar可以选择是否进行压缩,可选gzip和bzip2格式。使用 Phar 进行文件枚举、读取、include,无论是在本地硬盘还是 Samba 等网络文件共享挂载,性能都比零散的文件性能更强。

    PHP5.3+版本,Phar后缀文件是默认开启支持的,你不需要任何其他的安装就可以使用它。PHAR文件缺省状态是只读的,需要创建一个自己的Phar文件,所以需要允许写入Phar文件,这需要修改一下 php.ini。phar.readonly =0

    运行时配置

    这些函数的行为受php.ini中的设置影响。

    Filesystem and Streams Configuration Options
    名字默认可修改范围更新日志
    phar.readonly"1"PHP_INI_ALL
    phar.require_hash"1"PHP_INI_ALL
    phar.extract_list""PHP_INI_ALLAvailable from phar 1.1.0 to 1.2.3, removed in 2.0.0.
    phar.cache_list""PHP_INI_SYSTEMAvailable from phar 2.0.0.

    这是配置指令的简短说明。

    phar.readonlyboolean

    This option disables creation or modification of Phar archives using thepharstream or Phar object's write support. This setting should always be enabled on production machines, as the phar extension's convenient write support could allow straightforward creation of a php-based virus when coupled with other common security vulnerabilities.

    Note:

    This setting can only be unset in php.ini due to security reasons. Ifphar.readonlyis disabled in php.ini, the user may enablephar.readonlyin a script or disable it later. Ifphar.readonlyis enabled in php.ini, a script may harmlessly "re-enable" the INI variable, but may not disable it.

    phar.require_hashboolean

    This option will force all opened Phar archives to contain some kind of signature (currently MD5, SHA1, SHA256 and SHA512 are supported), and will refuse to process any Phar archive that does not contain a signature.

    Note:

    This setting can only be unset in php.ini due to security reasons. Ifphar.require_hashis disabled in php.ini, the user may enablephar.require_hashin a script or disable it later. Ifphar.require_hashis enabled in php.ini, a script may harmlessly "re-enable" the INI variable, but may not disable it.

    This setting does not affect reading plain tar files with the PharData class.

    phar.extract_liststring

    This INI setting has been removed as of phar 2.0.0

    Allows mappings from a full path to a phar archive or its alias to the location of its extracted files. The format of the parameter isname=archive,name2=archive2.This allows extraction of phar files to disk, and allows phar to act as a kind of mapper to extracted disk files. This is often done for performance reasons, or to assist with debugging a phar.

    Example #1 phar.extract_list usage example

    in php.ini:
    phar.extract_list = archive=/full/path/to/archive/,arch2=/full/path/to/arch2
    <?php
    include "phar://archive/content.php";
    include "phar://arch2/foo.php";
    ?>
    
    phar.cache_liststring

    This INI setting was added in phar 2.0.0

    Allows mapping phar archives to be pre-parsed at web server startup,providing a performance improvement that brings running files out of a phar archive very close to the speed of running those files from a traditional disk-based installation.

    Example #2 phar.cache_list usage example

    in php.ini (windows):
    phar.cache_list =C:\path\to\phar1.phar;C:\path\to\phar2.phar
    in php.ini (unix):
    phar.cache_list =/path/to/phar1.phar:/path/to/phar2.phar