yii模块化安装简易过程

<?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 '安装成功!';
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值