´o¸ÅÐeÇo£º
ÿһ¸o¶(C)µ¥¶¼Óжa¸oÎļþ¸½¼þ£¬ÔÚÏÂÔصÄʱºoÏ£Íu¶Ôµ±Ç°¶(C)µ¥µÄÎļþ×Ô¶¯´o°u³ÉÒ»¸oѹËo°uÏÂÔØ
ϸ½ÚÐeÇo£ºµ±Ç°¶(C)µ¥ºÅ_ÄeÔÂÈÕ+ʱ¼a.zip ÀýÈ磺
1.Éu³ÉѹËoÎļþ£¬Ñ¹ËoÎļþÃu¸ñʽ£º
2.ѹËoÎļþ´ae*ÅÔÚ¸uĿ¼ /upload/zipfile/ÄeÔÂ/×Ô¶¨ÒaµÄѹËoÎļþÃu.zip
3.µa»÷ÏÂÔØѹËo°u£¬ÏµÍ³¿ªÊ¼¶ÔѹËoÎļþ´o°u£¬´o°uÍe³Éºo×Ô¶¯¿ªÊ¼ÏÂÔØ
4.ΪÁËÀÖ¹±(C)¶ѹËo°uÎļþ¾¶£¬ÐeÒª¶ÔÏÂÔصÄѹËo°uÎļþÃu¸ÄÃu
¾ßÌa²Ù×÷ģʽÇe¼uÏÂÃaeµÄ´uÂe£º
ÎļþÂ*¾¶£º
ѹËo°uÎļþ´aež¶£º/upload/zipfile/
ÉÏ´«µÄ¸½¼þ´aež¶£º/upload/file/
1.»u±¾ÅaÖÃÎļþÎļþ config.inc.php*ÅÔÚϵͳ¸uĿ¼
define('SYS_ROOT', str_replace("\\", '/', dirname(__FILE__)));
define('SYS_UPLOAD', SYS_ROOT.'/upload/file');
define('SYS_DOWNLOAD', SYS_ROOT.'/upload/zipfile');
define('SYS_WIN', strpos(strtoupper(PHP_OS), 'WIN') !== false ? true: false);
define('SYS_CHMOD', ('0777' && !SYS_WIN) ? '0777' : 0);
2.ѹËo°u³ÌÐo´uÂeÎļþ getzip.php
header("Content-type: text/html; charset=utf-8");
require_once '../config.inc.php'; //ÔØÈeÅaÖÃÂ*¾¶ÅaÖÃÎļþ
$arrfiles = array(SYS_UPLOAD . '/1.jpg',
SYS_UPLOAD . '/x.jpg',); //ÕaÀiÊǸ½¼þµÄÎļþÊý×e
$orderNum = '888'; //¶(C)µ¥ºÅ
$downFileName = 'tieniu.zip'; //ÏÂÔصÄÎļþÃu Èç¹uΪ¿ÕÄÇô¾ÍÊÇϵͳ×Ô¶¨ÒaÃu³Æ Èç¹uÖ¸¶¨¾ÍÏÔʾָ¶¨Ãu×Ö
$zipUrl = create_zip($arrfiles, $orderNum); //Éu³ÉµÄѹËoÎļþÃu´Ê
file_down($zipUrl, $downFileName); //Ìa¹(C)httpÏÂÔØ£¬²¢¿ÉÒÔ½øÐÐÖØÃuÃuÏÂÔØÎļþ,½¨ÒeÖØÃuÃu£¬*ÀÖ¹Â*¾¶²Â½a
/*
* Éu³ÉѹËo°uÎļþÃu
* @param [String] $orderNum ¶(C)µ¥ºÅ
* @return [String] *µ»Ø´øÓоø¶ÔÂ*¾¶µÄ¶(C)µ¥ºÅµÄѹËoÎļþÃu
*/
function get_zipname($orderNum) {
$zipName = SYS_DOWNLOAD . '/' . date('Ym') . '/' . $orderNum . '_' . date("Ymd_Hi") . '.zip';
return $zipName;
}
/*
* °´ÕÕÌض¨ÐeÇo´o°uѹËo°uµÄĿ¼½a¹¹ÉeÖÃ
*/
function pack_object() {
}
/*
* Éu³ÉѹËo°u
* @param [Array] $arrfiles ´øÓоø¶ÔÂ*¾¶µÄÎļþÊý×e
* @param [String] $orderNum ¶(C)µ¥ºÅ
* @return [String] *µ»Ø´øÓоø¶ÔÂ*¾¶µÄ¶(C)µ¥ºÅµÄѹËoÎļþÃu ÈçÈç¹uʧ°Ü*µ»Ø FALSE
*/
function create_zip($arrfiles, $orderNum) {
$zipName = get_zipname($orderNum); //»ñµÃÎļþÃu
dir_create(dirname($zipName)); //½¨Á¢Éu³ÉѹËoÎļþµÄĿ¼
$zip = new ZipArchive();
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
return FALSE;
}
foreach ($arrfiles as $path) {
if (is_file($path)) {//ÅжÏÎļþÊÇ*ñ´aeÔÚ
$zip->addFile($path, basename($path)); //°ÑÎļþ¼ÓÈeµ½Ñ¹Ëo°uÖÐ
}
}
$zip->close();
return $zipName;
}
/*
* ´¦ÀiÎļþĿ¼
* @param [Array] $arrfiles ´øÓоø¶ÔÂ*¾¶µÄÎļþÊý×e
* @param [String] $dirpath ÎļþÂ*¾¶
* @return [String] *µ»Ø´¦ÀiµÄÎļþÂ*¾¶£¬*½±aÉu³ÉÎļþĿ¼
*/
function dir_path($dirpath) {
$dirpath = str_replace('\\', '/', $dirpath);
if (substr($dirpath, -1) != '/')
$dirpath = $dirpath . '/';
return $dirpath;
}
/*
* Éu³ÉÎļþĿ¼
* @param [String] $path ÎļþÂ*¾¶
* @return [String] *µ»ØÉu³ÉµÄÎļþĿ¼½a¹¹
*/
function dir_create($path) {
if (is_dir($path))
return true;
$dir = str_replace(SYS_DOWNLOAD . '/', '', $path);
$dir = dir_path($dir);
$temp = explode('/', $dir);
$cur_dir = SYS_DOWNLOAD . '/';
$max = count($temp) - 1;
for ($i = 0; $i < $max; $i++) {
$cur_dir .= $temp[$i] . '/';
if (is_dir($cur_dir))
continue;
@mkdir($cur_dir);
if (SYS_CHMOD)
@chmod($cur_dir, SYS_CHMOD);
if (!is_file($cur_dir . '/index.html') && !is_file($cur_dir . '/index.php'))
file_copy(SYS_ROOT . '/upload/index.html', $cur_dir . '/index.html');
}
return is_dir($path);
}
/*
* ÎļþCOPY
* @param [String] $from copyÔ´Îļþ
* @param [String] $to copyÎļþÄ¿µÄµØ
* @return [Bool] ³É¹¦ ture ʧ°Ü false
*/
function file_copy($from, $to) {
dir_create(dirname($to));
if (is_file($to) && SYS_CHMOD)
@chmod($to, SYS_CHMOD);
if (@copy($from, $to)) {
if (SYS_CHMOD)
@chmod($to, SYS_CHMOD);
return true;
} else {
return false;
}
}
/*
* ÎļþÏÂÔØ´¦Àiº¯Êý
* @param [String] $file ÎļþÂ*¾¶
* @param [String] $filename ÏÂÔØʱ¼aÖØÐÂÃuÃuµÄÎļþÃu
* @param [String] $data ÏÂÔØÎļþÌi×°µÄÊý¾ÝÄÚÈÝ
*/
function file_down($file, $filename = '', $data = '') {
if (!$data && !is_file($file))
exit;
$filename = $filename ? $filename : basename($file);
$filetype = file_ext($filename);
$filesize = $data ? strlen($data) : filesize($file);
ob_end_clean();
@set_time_limit(0);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
} else {
header('Pragma: no-cache');
}
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Encoding: none');
header('Content-Length: ' . $filesize);
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Type: ' . $filetype);
if ($data) {
echo $data;
} else {
readfile($file);
}
exit;
}
function file_ext($filename) {
return strtolower(trim(substr(strrchr($filename, '.'), 1)));
}
//´Ëº¯ÊýδÓõ½£¬ÓÃÀ´×oÕu¸oĿ¼µÄ´o°uÏÂÔØ
function listdir($start_dir = '.') {
$files = array();
if (is_dir($start_dir)) {
$fh = opendir($start_dir);
while (($file = readdir($fh)) !== false) {
if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0)
continue;
$filepath = $start_dir . '/' . $file;
if (is_dir($filepath))
$files = array_merge($files, listdir($filepath));
else
array_push($files, $filepath);
}
closedir($fh);
} else {
$files = false;
}
return $files;
}
3.PHP³ÌÐoÉu³ÉѹËoÎļþÐeÒªÓõ½Ñ¹ËoÀa£ºZipArchive
Õa¸oÊÇphpµÄÀ(C)Õ¹Àa£¬×Ôphp5.2°ae±¾ÒÔºo¾ÍÒѾ֧³ÖÕa¸oÀ(C)Õ¹£¬Èç¹uÄaÔÚʹÓõÄʱºo³oÏÖ´iÎo£¬²e¿´ÏÂphp.iniÀiÃaeµÄextension=php_zip.dllÇ°ÃaeµÄ*ÖºÅÓÐûÓÐÈ¥µo£¬È»ºoÔÙÖØÆoApacheÕaÑu²ÅÄÜʹÓÃÕa¸oÀa¿a¡£