How to scan files in a directory and enter them into the database in the Yii framework

不言
Release: 2023-04-02 22:30:02
Original
1644 people have browsed it

This article mainly introduces the method of scanning files in a directory into the database in the Yii framework. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

Requirements:

Write a scheduled task under the yii framework, scan the json file under a certain directory $target, and import it into the specified database

Implementation:

1. Enter the required The json file of the library is placed in the specified directory $target
2. Execute the scheduled task and import it into the mongo library Vote_teacher

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

3. Scheduled task code:
/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";

    }
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Yii cannot catch the exception solution

The above is the detailed content of How to scan files in a directory and enter them into the database in the Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!