您的位置:首页 > 编程语言 > PHP开发

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 '安装成功!';

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  yii模块化安装