yii模块化安装简易过程
2016-04-07 17:17
387 查看
<?php
class InstallController extends Controller {
//安装页面
public function actionIndex(){
$this->render('install');
}
//安装模块操作
public function actionUpload(){
//定义相关路径
$server_root = $_SERVER['CONTEXT_DOCUMENT_ROOT'];
define('DIR_UPLOAD', $server_root . '/uploadfiles/');
define('DIR_APPLICATION', $server_root . '/protected/');
$json = array();
//判断上传文件特点后缀格式,是否上传成功
if (!empty($_FILES['upload']['name'])) {
if (substr($_FILES['upload']['name'], -7) != '.gz.zip') {
$json['error'] = '文件格式错误';
}
if ($_FILES['upload']['error'] != 0) {
$json['error'] = '上传失败';
}
} else {
$json['error'] = '上传失败';
}
if (!$json) {
//随机生成文件夹名称
$path = 'temp-' . mt_rand(10000,99999);
$upload_path = DIR_UPLOAD . $path;
//不存在生成文件夹,给权限
if (!is_dir(DIR_UPLOAD . $path)) {
mkdir(DIR_UPLOAD . $path, 0777);
}
$file = DIR_UPLOAD . $path . '/upload.zip';
//把上传的文件移动到上传目录下
move_uploaded_file($_FILES['upload']['tmp_name'], $file);
if (file_exists($file)) {
$file = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $path) . '/upload.zip';
if (!file_exists($file)) {
$json['error'] = '文件不存在';
}
if (!$json) {
//解压文件夹
$zip = new ZipArchive();
if ($zip->open($file)) {
$zip->extractTo(DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $path));
$zip->close();
} else {
$json['error'] = '解压文件失败';
}
// Remove Zip
unlink($file);
}
$directory = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $path) . '/upload/';
if (!is_dir($directory)) {
$json['error'] = '文件夹不存在';
}
$files = array();
$path = array($directory . '*');
while (count($path) != 0) {
$next = array_shift($path);
foreach (glob($next) as $file) {
if (is_dir($file)) {
$path[] = $file . '/*';
}
$files[] = $file;
}
}
//先遍历文件夹,创建项目中不存在的文件
foreach ($files as $key => $file) {
$destination = substr($file, strlen($directory));
//如果是文件夹则判断是存在,存在就不做操作,否则创建
if (is_dir($file)) {
$web_dir = DIR_APPLICATION . $destination;
if (!is_dir($web_dir)) {
mkdir($web_dir, 0777, TRUE);
}
}
}
//遍历文件,移动文件
foreach ($files as $file) {
$destination = substr($file, strlen($directory));
if (is_file($file)) {
$web_file = DIR_APPLICATION . $destination;
if (!file_exists($web_file)) {
copy($file, $web_file);
}
}
}
//删除upload文件
Yii::import('application.models.web.FileModel');
$obj = new FileModel;
$res = $obj->deldir($upload_path);
echo '安装成功!';
}
}
}
}
class InstallController extends Controller {
//安装页面
public function actionIndex(){
$this->render('install');
}
//安装模块操作
public function actionUpload(){
//定义相关路径
$server_root = $_SERVER['CONTEXT_DOCUMENT_ROOT'];
define('DIR_UPLOAD', $server_root . '/uploadfiles/');
define('DIR_APPLICATION', $server_root . '/protected/');
$json = array();
//判断上传文件特点后缀格式,是否上传成功
if (!empty($_FILES['upload']['name'])) {
if (substr($_FILES['upload']['name'], -7) != '.gz.zip') {
$json['error'] = '文件格式错误';
}
if ($_FILES['upload']['error'] != 0) {
$json['error'] = '上传失败';
}
} else {
$json['error'] = '上传失败';
}
if (!$json) {
//随机生成文件夹名称
$path = 'temp-' . mt_rand(10000,99999);
$upload_path = DIR_UPLOAD . $path;
//不存在生成文件夹,给权限
if (!is_dir(DIR_UPLOAD . $path)) {
mkdir(DIR_UPLOAD . $path, 0777);
}
$file = DIR_UPLOAD . $path . '/upload.zip';
//把上传的文件移动到上传目录下
move_uploaded_file($_FILES['upload']['tmp_name'], $file);
if (file_exists($file)) {
$file = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $path) . '/upload.zip';
if (!file_exists($file)) {
$json['error'] = '文件不存在';
}
if (!$json) {
//解压文件夹
$zip = new ZipArchive();
if ($zip->open($file)) {
$zip->extractTo(DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $path));
$zip->close();
} else {
$json['error'] = '解压文件失败';
}
// Remove Zip
unlink($file);
}
$directory = DIR_UPLOAD . str_replace(array('../', '..\\', '..'), '', $path) . '/upload/';
if (!is_dir($directory)) {
$json['error'] = '文件夹不存在';
}
$files = array();
$path = array($directory . '*');
while (count($path) != 0) {
$next = array_shift($path);
foreach (glob($next) as $file) {
if (is_dir($file)) {
$path[] = $file . '/*';
}
$files[] = $file;
}
}
//先遍历文件夹,创建项目中不存在的文件
foreach ($files as $key => $file) {
$destination = substr($file, strlen($directory));
//如果是文件夹则判断是存在,存在就不做操作,否则创建
if (is_dir($file)) {
$web_dir = DIR_APPLICATION . $destination;
if (!is_dir($web_dir)) {
mkdir($web_dir, 0777, TRUE);
}
}
}
//遍历文件,移动文件
foreach ($files as $file) {
$destination = substr($file, strlen($directory));
if (is_file($file)) {
$web_file = DIR_APPLICATION . $destination;
if (!file_exists($web_file)) {
copy($file, $web_file);
}
}
}
//删除upload文件
Yii::import('application.models.web.FileModel');
$obj = new FileModel;
$res = $obj->deldir($upload_path);
echo '安装成功!';
}
}
}
}
相关文章推荐
- [扩展推荐]Laravel User Agent 轻松识别客户端信息
- 初识ThinkPHP控制器
- cookie存取
- PHP对大文件的处理思路
- [Phonegap+Sencha Touch] 移动开发29 安卓navigator.camera.getPicture得到图片的真实路径
- ssh2 php扩展
- PHP正则表达式
- PHP 生成图片缩略图函数
- phpmyadmin #1045 - Access denied for user 'root'@'localhost' (using password: NO)
- ftp服务安装
- php中socket的使用
- php与java通用AES加密解密算法
- PHP header函数大全
- REST介绍与REST在PHP中的应用
- phpcms学习的一点心得
- 十进制与三十六机制 互转 PHP代码
- PHP中使用cURL实现Get和Post请求的方法
- PHP获取今天、昨天、明天的日期
- PHP内核探索——入门
- 利用PHPMailer 来完成PHP的邮件发送