/*
- 生成压缩包文件名
- @param [String] $orderNum 订单号
- @return [String] 返回带有绝对路径的订单号的压缩文件名
*/
function get_zipname($orderNum) {
$zipName = SYSDOWNLOAD . '/' . date('Ym') . '/' . $orderNum . '' . date("Ymd_Hi") . '.zip';
return $zipName;
}
/*
function pack_object() {
}
/*
- 生成压缩包
- @param [Array] $arrfiles 带有绝对路径的文件数组
- @param [String] $orderNum 订单号
- @return [String] 返回带有绝对路径的订单号的压缩文件名 如如果失败返回 FALSE
*/
function create_zip($arrfiles,$orderNum) {
$zipName = get_zipname($orderNum); //获得文件名
dir_create(dirname($zipName)); //建立生成压缩文件的目录
$zip = new ZipArchive();
if ($zip->open($zipName,ZIPARCHIVE::CREATE) !== TRUE) {
return FALSE;
}
foreach ($arrfiles as $path) {
if (is_file($path)) {//判断文件是否存在
$zip->addFile($path,basename($path)); //把文件加入到压缩包中
}
}
$zip->close();
return $zipName;
}
/*
- 处理文件目录
- @param [Array] $arrfiles 带有绝对路径的文件数组
- @param [String] $dirpath 文件路径
- @return [String] 返回处理的文件路径,方便生成文件目录
*/
function dir_path($dirpath) {
$dirpath = str_replace('',$dirpath);
if (substr($dirpath,-1) != '/')
$dirpath = $dirpath . '/';
return $dirpath;
}
/*
- 生成文件目录
- @param [String] $path 文件路径
- @return [String] 返回生成的文件目录结构
*/
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;
}
}
/*
- 文件下载处理函数
- @param [String] $file 文件路径
- @param [String] $filename 下载时间重新命名的文件名
- @param [String] $data 下载文件填装的数据内容
*/
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)));
}
//此函数未用到,用来做整个目录的打包下载
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程序生成压缩文件需要用到压缩类:ZipArchive
这个是php的扩展类,自php5.2版本以后就已经支持这个扩展,如果你在使用的时候出现错误,查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉,然后再重启Apache这样才能使用这个类库。
(编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!