首页  >  文章  >  后端开发  >  在yii框架中扫描目录下文件入数据库的方法

在yii框架中扫描目录下文件入数据库的方法

不言
不言原创
2018-07-10 15:20:341660浏览

这篇文章主要介绍了关于在yii框架中扫描目录下文件入数据库的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

需求:

带yii框架下写一个定时任务,扫描某一目录$target下的json文件,并导入指定的数据库中

实现:

1.把需要入库的json文件放在指定目录$target下
2.执行定时任务,导入到mongo库Vote_teacher中

/opt/modules/php/bin/php /www/web/protected/yiic.php import VoteTeacher

3.定时任务代码:
/web/protected/commandsImportCommand.php

 /tmp/abc1.txt
 */
class ImportCommand extends CConsoleCommand 
{

    public $target='/www/web/html/import/';     //扫描的目录

    public $model;    //入库的数据库对象

    public function run($args) {

        if (isset($args[0])) {
            $this->showMessage("params error", 1);
            exit;
        }
        $this->model = $args[0];
        $this->import();
    }

    /**
     * 分析用户回答的信息并按格式入库vote_answer
     * @author      lizhihui
      * @date        2018-4-9
     */
    private function import() 
    {
        $files = scandir($this->target);
        //读取目录下文件
        foreach ($files as $key => $val) {
            $val = pathinfo($val);
            $extension = $val['extension'];
            //过滤掉非json格式
            if($extension!='json'){
                continue;
            }

            $filePath=$this->target.$val['basename'];
            $fp=fopen($filePath,'r');
            $content='';
            while (!feof($fp)){
                $content.=fread($fp, filesize($filePath)/10);//每次读出文件10分之1
            //进行处理
            }
            $arr=json_decode($content);
            if(empty($arr)){
                $this->showMessage("no data to import");
                die();
            }

            //实例化不同数据库
            $tag=true;
            foreach ($arr as $key => $val) {                
                //存储
                $aVal = (array)$val;
                $model = new $this->model;
                if($model instanceof SMongodb){//动态字段存储
                    foreach ($aVal as $k => $v) {
                        $model->$k=$v;
                    }
                }else{//非动态字段存储
                    $model->attributes=$aVal;
                }

                if(!$model->save()){
                    $tag=false;
                }else{
                    unset($model);    //销毁一个对象,再次使用的时候会new一个新的
                }
            }

        }
        if(!$tag){
            $this->showMessage("some error in import");
        }else{
            $this->showMessage('import success!');
        }
    }    


    /**
     * 信息输出
     * @author      lizhihui
      * @date        2018-4-9
     */
    private function showMessage($str, $err = 0) {
        if (!$str) {
            return false;
        }
        if ($err) {
            echo "[ERROR]";
        } else {
            echo "[SUCCESS]";
        }

        echo date("Y-m-d H:i:s", time()) . " " . $str . "\n";

    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

Yii无法捕获到异常的解决方法

以上是在yii框架中扫描目录下文件入数据库的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn