略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Phar::buildFromDirectory

2024-11-13

Phar::buildFromDirectory

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 2.0.0)

Phar::buildFromDirectoryConstruct a phar archive from the files within a directory

说明

public Phar::buildFromDirectory(string $directory, string $pattern = ""): array

注意:

此方法需要 将 php.ini 中的 phar.readonly 设为 0 以适合 Phar 对象. 否则, 将抛出PharException.

Populate a phar archive from directory contents. The optional second parameter is a regular expression (pcre) that is used to exclude files. Any filename that matches the regular expression will be included, all others will be excluded. For more fine-grained control, use Phar::buildFromIterator().

参数

directory

The full or relative path to the directory that contains all files to add to the archive.

pattern

An optional pcre regular expression that is used to filter the list of files. Only file paths matching the regular expression will be included in the archive.

返回值

Phar::buildFromDirectory() returns an associative array mapping internal path of file to the full path of the file on the filesystem.

错误/异常

This method throws BadMethodCallException when unable to instantiate the internal directory iterators, or a PharException if there were errors saving the phar archive.

更新日志

版本 说明
8.1.0 Phar::buildFromDirectory() no longer returns false.

范例

示例 #1 A Phar::buildFromDirectory() example

<?php
// create with alias "project.phar"
$phar = new Phar('project.phar'0'project.phar');
// add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
$phar->setStub($phar->createDefaultStub('cli/index.php''www/index.php'));

$phar2 = new Phar('project2.phar'0'project2.phar');
// add all files in the project, only include php files
$phar2->buildFromDirectory(dirname(__FILE__) . '/project''/\.php$/');
$phar2->setStub($phar->createDefaultStub('cli/index.php''www/index.php'));
?>

参见

add a noteadd a note

User Contributed Notes 1 note

up
6
dalfarra at codref dot com
5 years ago
If you want to exclude a directory from the archive (but include all the other content), the regular expression must take into consideration the whole path of the file, not just the file or directory name relative to the source folder.

As an example, if we want to exclude "nbproject" directory from the archive (and any occurrence of it):

/tmp/myfolder
  /nbproject
  /something
  /something-else
    /nbproject
  /file1.php
  /file2.php

the regex should be:
<?php
  $exclude
= '/^(?!(.*nbproject))(.*)$/i'//ignoring the case
?>

A more complete example:
<?php
    $archive_file
= 'myarchive.tar';
   
$folder_to_compress = '/tmp/myfolder';
   
$archive = new PharData($archive_file);
   
$exclude = '/^(?!(.*nbproject))(.*)$/i'
   
$archive->buildFromDirectory($folder_to_compress,$exclude);
   
$archive->compress(Phar::GZ);
   
unlink($archive_file); // as we already obtained a tar.gz
?>

the archive will contain:
/
  /something
  /something-else
  /file1.php
  /file2.php

官方地址:https://www.php.net/manual/en/phar.buildfromdirectory.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3