函式名:Phar::extractTo()
適用版本:PHP 5 >= 5.3.0, PHP 7, PHP 8
函式描述:Phar::extractTo() 方法用於將整個Phar檔案或指定的檔案從Phar存檔中提取到檔案系統。
語法:bool Phar::extractTo(string $directory [, string|array $files [, bool $overwrite = false ]])
引數:
- $directory:提取檔案的目標目錄路徑。
- $files(可選):要提取的特定檔案或檔案列表。如果未指定,則將提取整個Phar存檔。
- $overwrite(可選):是否允許覆蓋現有檔案。預設為false,不允許覆蓋。
返回值:如果提取成功,則返回true;否則返回false。
示例:
<?php
$phar = new Phar('example.phar');
// 提取整個Phar存檔到目標目錄
$phar->extractTo('/path/to/destination');
// 提取特定檔案到目標目錄
$phar->extractTo('/path/to/destination', 'file.txt');
// 提取多個檔案到目標目錄
$phar->extractTo('/path/to/destination', ['file1.txt', 'file2.txt']);
// 允許覆蓋現有檔案
$phar->extractTo('/path/to/destination', null, true);
?>
上述示例中,首先建立了一個名為example.phar的Phar存檔物件。然後使用extractTo()方法將整個Phar存檔提取到目標目錄'/path/to/destination'。接下來,示例展示瞭如何提取特定檔案或多個檔案到目標目錄,並且還演示瞭如何允許覆蓋現有檔案。
請注意,使用該函式需要確保目標目錄有適當的寫入許可權,否則將會導致提取失敗。