很多时候我们可能需要将很多小文件打包提供给用户下载,那具体如何实现呢?本文主要介绍了php打包文件实例,直接给出php打包文件的示例代码,希望对大家有所帮助。
大概需求:
每一个订单都有多个文件附件,在下载的时候希望对当前订单的文件自动打包成一个压缩包下载
细节需求:当前订单号_年月日+时间.zip 例如:
1.生成压缩文件,压缩文件名格式:
2.压缩文件存放在根目录 /upload/zipfile/年月/自定义的压缩文件名.zip
3.点击下载压缩包,系统开始对压缩文件打包,打包完成后自动开始下载
4.为了防止暴露压缩包文件路径,需要对下载的压缩包文件名改名
具体操作模式请见下面的代码:
文件路径:
压缩包文件存放路径:/upload/zipfile/
上传的附件存放路径:/upload/file/
1.基本配置文件文件 config.inc.php放在系统根目录
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.压缩包程序代码文件 getzip.php
header("content-type: text/html; charset=utf-8");
require_once '../config.inc.php'; //载入配置路径配置文件
$arrfiles = array(sys_upload . '/1.jpg',
sys_upload . '/x.jpg',); //这里是附件的文件数组
$ordernum = '888'; //订单号
$downfilename = 'tieniu.zip'; //下载的文件名 如果为空那么就是系统自定义名称 如果指定就显示指定名字
$zipurl = create_zip($arrfiles, $ordernum); //生成的压缩文件名词
file_down($zipurl, $downfilename); //提供http下载,并可以进行重命名下载文件,建议重命名,防止路径猜解
/*
* 生成压缩包文件名
* @param [string] $ordernum 订单号
* @return [string] 返回带有绝对路径的订单号的压缩文件名
*/
function get_zipname($ordernum) {
$zipname = sys_download . '/' . 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这样才能使用这个类库。
相关推荐:
php 文件分割与合并(断点续传)
php 文件类型的判断示例代码
简单谈谈 php 文件锁
以上就是php打包文件实例的详细内容。