ZipArchive::addPattern
(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL zip >= 1.9.0)
ZipArchive::addPattern — Add files from a directory by PCRE pattern
Description
public ZipArchive::addPattern(string
$pattern, string $path = ".", array $options = []): array|falseAdd files from a directory which match the regular expression pattern. The operation is not recursive. The pattern will be matched against the file name only.
Parameters
patternA PCRE pattern against which files will be matched.
pathThe directory that will be scanned. Defaults to the current working directory.
optionsAn associative array of options accepted by ZipArchive::addGlob().
Examples
Example #1 ZipArchive::addPattern() example
Add all php scripts and text files from current directory
<?php
$zip = new ZipArchive();
$ret = $zip->open('application.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($ret !== TRUE) {
printf('Failed with code %d', $ret);
} else {
$directory = realpath('.');
$options = array('add_path' => 'sources/', 'remove_path' => $directory);
$zip->addPattern('/\.(?:php|txt)$/', $directory, $options);
$zip->close();
}
?>See Also
- ZipArchive::addFile() - Adds a file to a ZIP archive from the given path
- ZipArchive::addGlob() - Add files from a directory by glob pattern
↑ and ↓ to navigate • Enter to select • Esc to close • / to open