Hướng dẫn php zip->addfile
zip là một định dạng nén phổ biến, được sử dụng để làm giảm dung lượng các định dạng file, để có thể dễ dàng gửi chúng đi nơi khác. PHP sử dụng class ZipArchive để hỗ trợ nén một hoặc nhiều file tới một file zip. Zip 1 fileopen('output/data.zip', ZipArchive::CREATE || ZipArchive::OVERWRITE)) { // Add files if (!$zipArchive->addFile('data/test-1.txt', 'test-1.txt')) { printf("zip file error"); exit; } } else { printf('Open file error'); exit; } // Close ZipArchive $zipArchive->close(); Kết quả: Zip 1 folderopen('output/data.zip', ZipArchive::CREATE || ZipArchive::OVERWRITE)) { // Add folder $dir = opendir('data/'); while($file = readdir($dir)) { if(is_file("data/${file}")) { if (!$zipArchive->addFile("data/${file}", $file)) { printf("zip file error"); exit; } } } } else { printf('Open file error'); exit; } // Close ZipArchive $zipArchive->close(); Kết quả: Unzip fileopen('output/data.zip')) { // Extracts to current directory $zipArchive->extractTo('./'); } else { printf('Open file error'); exit; } // Close ZipArchive $zipArchive->close(); Nguồn: vinasupport.com (PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL zip >= 1.1.0) ZipArchive::addFile — Adds a file to a ZIP archive from the given path Descriptionpublic ZipArchive::addFile(
Parametersfilepath The path to the file to add. entryname If supplied and not empty, this is the local name inside the ZIP archive that will override the start For partial copy, start position. length For partial copy, length to be copied, if 0 or -1 the whole file
(starting from flags Bitmask consisting of Return Values Returns Changelog
ExamplesThis example opens a ZIP file archive test.zip and add the file /path/to/index.txt. as newname.txt. Example #1 Open and add
Notes
jayarjo ¶ 12 years ago
->addFile($abs_path, $relative_path);
?>
john factorial ¶ 11 years ago
aartdebruijn at gmail dot com ¶ 11 years ago
frame86 at live dot com ¶ 8 years ago
romuloum at hotmail dot com ¶ 8 years ago
wacher at freemail dot hu ¶ 14 years ago
garcia at no_span dot krautzer-lynn dot com ¶ 13 years ago
camuc at camuc dot net ¶ 12 years ago
peter at boring dot ch ¶ 13 years ago
addDir($path) {
kris at blacksuitmedia [do/t/] c0m ¶ 9 years ago
$zip->numFiles == $filelimit) {$zip->close(); $zip->open($file) or die ("Error: Could not reopen Zip");} Anonymous ¶ 12 years ago
ohcc at 163 dot com ¶ 6 years ago
sp at read dot eu ¶ 10 years ago
610010559 at qq dot com ¶ 6 months ago
stanislav dot eckert at vizson dot de ¶ 7 years ago
raja at rsdisk dot com ¶ 3 years ago
gio AT giombg dot com ¶ 4 years ago
christophe dot braud at aquafadas dot com ¶ 13 years ago
Farzad Ghanei ¶ 13 years ago
todd at toddwiggins dot com dot au ¶ 8 years ago
s dot eckert dot spam at gmx dot com ¶ 8 years ago
Dean Rather ¶ 9 years ago
shano ¶ 10 years ago
Andreas R. newsgroups2005 at geekmail de ¶ 15 years ago
ptipti at gala dot net ¶ 10 years ago
javierseixas at gmail dt com ¶ 14 years ago
ohcc at 163 dot com ¶ 6 years ago
= new ZipArchive;$file = '吴先成.txt'; mike at thetroubleshooters dot dk ¶ 14 years ago
stanleyshilov {} gmail.com ¶ 14 years ago
arrtedone at gmail dot com ¶ 9 years ago
ss at littlerain dot com ¶ 13 years ago
marco at maranao dot ca ¶ 13 years ago
pelpet at ic dot cz ¶ 12 years ago
|