public function download_backup() { ini_set('memory_limit', '228M'); $this->load->library('HZip'); $file_name = "backup_file/BK_image_" . date("Ymd_his") . ".zip"; if (HZip::zipDir(FCPATH . "asset/", FCPATH . $file_name)) { echo json_encode(["status" => "done", "url" => "/" . $file_name]); } else { echo json_encode(["status" => "error"]); } // $this->load->library('zip'); // $path = FCPATH."asset/images"; // $this->zip->read_dir($path); // $path = FCPATH."asset/uploads"; // $this->zip->read_dir($path); // $path = APPPATH."models/db"; // $this->zip->read_dir($path); // $file_name = "backup_file/".date("Y_m_d__H_i_s")."_backup_folder_asset.zip"; // if($this->zip->archive(FCPATH.$file_name)==true){ // echo json_encode(["status"=>"done","url"=> "/".$file_name]); // }else{ // echo json_encode(["status"=>"error"]); // } }
/** * 下载文件 */ public function download_site() { $site_id = I('site_id', session('site_info.id')); if (empty($site_id)) { return $this->error('请选择网站'); } $site_info = M()->table('site_info')->field('site_name,url,json')->where(array('id' => $site_id, 'user_id' => session('user_info.id'), 'status' => 0))->find(); if (empty($site_info)) { return $this->error('此网站无效'); } $info = M()->table('user_column')->where(array('site_id' => $site_id, 'forbidden' => 0))->order('sort')->select(); if (empty($info)) { return $this->error('没有找到文件'); } $rootpath = C('TEMP_DIR') . $site_info['url'] . "/"; if (is_dir($rootpath)) { deleteAll($rootpath, true); } elseif (!mkdir($rootpath)) { return $this->error('创建根目录失败'); } $public_rootpath = $rootpath . "Public/"; if (!mkdir($public_rootpath)) { return $this->error('创建Public目录失败'); } /*=================================生成html==============================*/ $html_rootpath = $rootpath . "html/"; if (!mkdir($html_rootpath)) { return $this->error('创建html目录失败'); } $user_info = M()->table('user_info')->field('nickname,head_img')->find(session('user_info')['id']); $site_common = A('Panel')->get_site_common($site_id); $theme_templet = $site_common['theme_templet']; unset($site_common['theme_templet']); $this->collect_link($site_common['theme_links']); //var_dump($site_common['theme_links']); $this->assign($site_common); $this->assign('user_info', $user_info); $this->assign('download', true); /*==================生成栏目页===================*/ foreach ($info as $key => $value) { $widget_common = A('Panel')->resolve_json($site_id, $value['html']); $this->collect_link($widget_common['links']); $links = array_merge_recursive($site_common['theme_links'], $widget_common['links']); $this->assign($links); $this->assign('now_column', $value['id']); $this->assign('content', $widget_common['content']); $html = $this->fetch('Theme/theme'); $html = $this->replaceHtml($html); $result = file_put_contents($html_rootpath . $value['url'] . '.html', $html); if (!$result) { ////***删除文件**/// deleteAll($rootpath); return $this->error('下载失败:html失败'); } } /*==================生成详情页===================*/ $desc = json_decode($site_info['json'], true); foreach ($desc as $key => $value) { $desc_info = D($key)->get_all_info($site_id); $desc_path = $rootpath . '/' . $key . '/'; if (!mkdir($desc_path)) { deleteAll($rootpath); return $this->error('创建$key目录失败'); } $widget = A('Panel')->load_widget(ucwords(strtolower($key)) . 'Desc', $value); $widget_link = $widget->load_template_link(); $this->collect_link($widget_link, $rootpath); $links = array_merge_recursive($site_common['theme_links'], $widget_link); $this->assign($links); foreach ($desc_info as $desc_key => $desc_resource) { ob_start(); ob_implicit_flush(0); $widget->index($site_id, null, $desc_resource); $content = ob_get_clean(); \Think\Hook::listen('view_filter', $content); $this->assign('content', $content); $html = $this->fetch('Theme/theme'); $html = $this->replaceHtml($html); $result = file_put_contents($desc_path . $desc_resource['id'] . '.html', $html); if (!$result) { ////***删除文件**/// deleteAll($rootpath); return $this->error('下载失败:article失败'); } } } /*=================================引入资源文件==============================*/ /*=============引入js css=============*/ $this->download_theme_link($public_rootpath, $site_common['site_ifno']['theme']); $this->download_widget_link($public_rootpath); /*=============引入img=============*/ $img_path = $rootpath . 'Uploads/'; $photo_info = M()->table('photo as a')->field('c.savename,c.savepath')->join('home_picture as c on a.pic_id = c.id')->where(array('a.site_id' => $site_id))->select(); $admin_column_info = M()->table('user_column as a')->field('b.savename,b.savepath')->join('left join picture as b on a.icon = b.id')->where(array('a.site_id' => $site_id, 'a.is_default' => 1))->select(); $home_column_info = M()->table('user_column as a')->field('b.savename,b.savepath')->join('left join home_picture as b on a.icon = b.id')->where(array('a.site_id' => $site_id, 'a.is_default' => 0))->select(); $article_info = M()->table('article as a')->field('b.savename,b.savepath')->join('left join home_picture as b on a.pic_id = b.id')->where(array('a.site_id' => $site_id))->select(); $img_info = array_merge($photo_info, $admin_column_info, $home_column_info, $article_info); $uploads_path = C('PICTURE_UPLOAD')['rootPath']; foreach ($img_info as $key => $value) { hCopy($uploads_path . $value['savepath'] . $value['savename'], $img_path . $value['savepath'] . $value['savename']); } /*========================生成zip==============================*/ load("@.HZip#class"); $zip_name = C('TEMP_DIR') . $site_info['url'] . '.zip'; $zip = \HZip::zipDir(C('TEMP_DIR') . $site_info['url'], $zip_name); /*========================下载==================================*/ header("Cache-Control: max-age=0"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename=' . basename($zip_name)); // 文件名 header("Content-Type: application/zip"); // zip格式的 header("Content-Transfer-Encoding: binary"); //二进制文件 header('Content-Length: ' . filesize($zip_name)); // 告诉浏览器,文件大小 @readfile($zip_name); //输出文件; unlink($zip_name); deleteAll($rootpath); }
<?php /** * @author Nguyen Duc Hanh * @copyright 2014 * @copygith nguyenduchanh.com */ include_once 'hzip.class.php'; include_once 'functions.php'; $folder = isset($_REQUEST['f']) ? $_REQUEST['f'] : ''; if ($folder) { HZip::zipDir("save/{$folder}", "save/{$folder}.zip"); $site_path = get_site_path(); header("location: {$site_path}/save/{$folder}.zip"); }
/** * [backup_file_images_family] * Không dùng hàm này nữa */ public function backup_file_images_family($options = null) { return; $this->load->library('HZip'); $file_name = "BK_image_" . date("Ymd_his") . ".zip"; HZip::zipDir(FCPATH . "asset/images", FCPATH . "asset/tmp/" . $file_name); $files = scandir(FCPATH . "asset/tmp"); if (count($files) > 7) { $l_file = []; foreach ($files as $key => $value) { if (!is_dir(FCPATH . "asset/tmp/" . $value)) { echo "<p>" . $value . "</p>"; preg_match("/(\\d+)_(\\d+)/", $value, $match); if ($match[1]) { $l_file[] = $value; } } } if (!empty($l_file)) { sort($l_file); unlink(FCPATH . "asset/tmp/" . $l_file[0]); } } if (ALLOW_SENT_MAIL) { if (file_exists(FCPATH . "asset/tmp/" . $file_name)) { $this->load->library('email', $this->config_email_custom); $this->email->from(FROM_EMAIL, 'Family'); $this->email->to('*****@*****.**'); $this->email->cc('*****@*****.**'); $this->email->subject("Backup file images " . date("Y-m-d h:i:s")); $this->email->message("<h2>Backup file images " . date("Y-m-d h:i:s") . "</h2> <p>Link:" . base_url() . "asset/tmp/" . $file_name . "</p>"); if ($this->email->send()) { echo "<h1>Send mail [" . __FUNCTION__ . "]</h1>"; } else { echo "<h1>Can't sent mail [" . __FUNCTION__ . "]</h1>"; } } else { echo "<h1>Don't have file [" . __FUNCTION__ . "]</h1>"; } } }
} else { echo 'exit'; exit; } } if (file_exists($file_path)) { if (!is_dir($file_path)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . urldecode(basename($file_path)) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file_path)); readfile($file_path); exit; } else { $tmp_zip = tempnam('/tmp', hash('crc32', $file_path)) . '.zip'; HZip::zipDir($file_path, $tmp_zip); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . urldecode(basename($file_path)) . '.zip"'); header("Content-Transfer-Encoding: binary"); header('Content-Length: ' . filesize($tmp_zip)); readfile($tmp_zip); exit; } } else { echo $file_path; echo 'not exist'; }
public static function deleteDirectory($dir) { if (!file_exists($dir)) { return true; } if (!is_dir($dir) || is_link($dir)) { return unlink($dir); } foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') { continue; } if (!HZip::deleteDirectory($dir . "/" . $item)) { chmod($dir . "/" . $item, 0777); if (!HZip::deleteDirectory($dir . "/" . $item)) { return false; } } } return rmdir($dir); }