赞
踩
HTML
common.php// 应用公共文件
/**
* 循环压缩目录文件
* @param $path 文件夹路径
* @param $zip 压缩后zip名字及路径
*/
function addFileToZip($path, $zip)
{
header("Content-type:text/html;charset=gbk");
$arr = scandir($path);
// print_r($arr);die;
$i = 1;
foreach ($arr as $val){
if ($val != "." && $val != "..") {
if (is_dir($path . "/" . $val)) {
addFileToZip($path . "/" . $val, $zip);
} else {
//将文件加入zip对象
$zip->addFile($path . "/" . $val,iconv('utf-8','gbk//ignore',$val));
// $zip->addFile($path . "/" . $val,$val);
}
}
}
return;
}
/**
* 压缩目录
* $file 压缩文件名
* $path 压缩的文件夹路径
*/
function yasuo($path,$file)
{
$zip = new \ZipArchive();
// $username = $_SESSION['adminUser'];
$zipname = $file . '.zip';
$zipPath = $path.'/' . $zipname;
if ($zip->open("$zipPath", \ZipArchive::CREATE) === TRUE) {
addFileToZip($path, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close(); //关闭处理的zip文件
//设置打包完自动下载
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipPath));
readfile($zipPath);
unlink($zipPath);
}
}
/**
* 删除文件夹
*/
function deleteDir($dir)
{
if (!$handle = @opendir($dir)) {
return false;
}
while (false !== ($file = readdir($handle))) {
if ($file !== "." && $file !== "..") { //排除当前目录与父级目录
$file = $dir . '/' . $file;
if (is_dir($file)) {
deleteDir($file);
} else {
@unlink($file);
}
}
}
@rmdir($dir);
}
控制器class Index extends Controller
{
public function index()
{
return $this->fetch();
}
//导出图片
public function daochu(){
ini_set("memory_limit","-1");
set_time_limit(0);
//导出数据,实际数据可以是从mysql导出
$data = [];
$data[] = ['id'=>1,'name'=>'张三','shouji'=>'xxx','qq'=>'\'111','weixin'=>'222'];
$data[] = ['id'=>2,'name'=>'李四','shouji'=>'xxx','qq'=>'\'111','weixin'=>'222'];
$time = date('YmdHi');
$path = 'upload/images/'.$time;
//如果目标
if (!is_dir($path)){
mkdir($path);
}
$arr = [
'http://phpdaima.com/uploads/item/201311/19/20131119204800_HLkRx.thumb.1900_0.jpeg',
'http://phpdaima.com/uploads/blog/201401/02/20140102112513_zHitZ.thumb.1000_0.jpeg'
];
//读取图片到服务器目录
foreach ($arr as $val){
$arr = pathinfo($val);
$file = md5($val);
$file = $file.'.'.$arr['extension'];
if (!file_exists($path.'/'.$file)){
$cont = file_get_contents($val);
file_put_contents($path.'/'.$file,$cont);
}
}
//压缩包名称
$file = $time;
//生成压缩包
yasuo($path,$file);
//删除图片
deleteDir($path);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。