For me the better way seems to be:
$options = array('add_path' => DIRECTORY_SEPARATOR, 'remove_all_path' => TRUE);
On Windows uses \, on others /
(PHP 5 >= 5.3.0, PHP 7, PECL zip >= 1.9.0)
ZipArchive::addGlob — ディレクトリから、glob パターンを使ってファイルを追加する
$pattern
[, int $flags
= 0
[, array $options
= array()
]] ) : bool
ディレクトリから、glob pattern
にマッチするファイルを追加します。
注意: ポータビリティを考慮して、ZIP ファイル名のディレクトリ区切り文字には常にスラッシュ (/) を使うことを推奨します。
pattern
glob() パターン。どのファイルを対象にするのかを指定します。
flags
glob() のフラグのビットマスク。
options
オプションの連想配列。次のオプションが使えます。
"add_path"
アーカイブ内のファイルのローカルパスに変換するときにつけるプレフィックス。 これが適用されるのは、 "remove_path" や "remove_all_path" で定義された削除処理がすべて終わった後です。
"remove_path"
マッチしたファイルをアーカイブに追加する前に削除するプレフィックス。
"remove_all_path"
TRUE
にすると、ファイル名だけを使ってアーカイブのルートに追加します。
成功した場合に TRUE
を、失敗した場合に FALSE
を返します。
例1 ZipArchive::addGlob() の例
現在の作業ディレクトリにある、すべての PHP スクリプトとテキストファイルを追加します。
<?php
$zip = new ZipArchive();
$ret = $zip->open('application.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($ret !== TRUE) {
printf('Failed with code %d', $ret);
} else {
$options = array('add_path' => 'sources/', 'remove_all_path' => TRUE);
$zip->addGlob('*.{php,txt}', GLOB_BRACE, $options);
$zip->close();
}
?>
For me the better way seems to be:
$options = array('add_path' => DIRECTORY_SEPARATOR, 'remove_all_path' => TRUE);
On Windows uses \, on others /
Neither 'remove_all_path' or 'remove_path' options seem to be workng
Yes!! neither 'remove_all_path' or 'remove_path' be working,if you want the right result,you should like this, $option = array( 'add_path' => '/', 'remove_all_path' => 'your path' ); 'add_path' just give a '/', then you will (; 。
Doesn't work with the following options:
$options = array('remove_all_path' => TRUE);
$zipArchive->addGlob($path."/*", GLOB_BRACE, $options);
The full path to file is adding to the archive.
Works well if:
$options = array('add_path' => './','remove_all_path' => TRUE);
As others suggested, to remove all paths, use the space as basepath.
Zip opened with builtin utility on a Windows XP and WinRAR.
$options = array('add_path' => ' ','remove_all_path' => TRUE);
Thanks for all contributers.