Skip to main content

Một PHP class tạo file ZIP

Published 1 min readMarkdown

Tôi đang phải làm chức năng nén/giải nén trong dự án hiện tại. Giải nén thì đã làm xong rồi, còn nén nữa là xong. Zip library của PHP hỗ trợ chức năng zip hơi bị kém, cái cơ bản nhất là cũng ngại phải viết => search & thấy class này khá nhẹ để nén 1 folder.

$zipfile = new zipfile();

rec_listFiles('myFolder');

header("Content-type: application/octet-stream"); header("Content-disposition: attachment; filename=zipfile.zip"); echo $zipfile->file();

function rec_listFiles( $from = '.') { global $zipfile, $filedata;

if(! is_dir($from)) return false;

if( $dh = opendir($from)) { while( false !== ($file = readdir($dh))) { // Skip '.' and '..' if( $file == '.' || $file == '..') continue; $path = $from . '/' . $file; if( is_dir($path) ) { $zipfile->add_dir($path); rec_listFiles($path); } else { $filedata = implode("", file($path)); $zipfile->add_file($filedata, $path); } } closedir($dh); } } ?>

Tạm đáp ứng yêu cầu :)

Source: http://www.devco.net/archives/2005/05/24/creating_zip_files_with_php.php

About the author

  • Khanh is a full stack web developer with over 15 years of experience developing for the web

No comments yet. Be the first to comment.

More from this blog