php读取文件夹文件,清理文件

// 获取文件夹中图片的数量和名称
    public function show_tmp_file() {
        $handler = opendir('./tmp/');//当前目录中的文件夹下的文件夹
        $count = 0;
        $filesize = 0;
        $imgArr = [];
        while( ($filename = readdir($handler)) !== false ) {
            if($filename != "." && $filename != ".." && $filename != "qrcode.png"){
                $imgArr[$count] = $filename;
                $filesize += filesize('./tmp/'.$filename);
                ++$count;
            }
        }

        $outData['count'] = $count;
        $outData['filesize'] = round($filesize / 1000 /1000,2) ."M";
        $outData['data']  = $imgArr;

        dump($outData);

        closedir($handler);
    }

    // 清理无用的图片
    public function clear_tmp_file() {
        $handler = opendir('./tmp/');//当前目录中的文件夹下的文件夹
        $count = 0;
        $imgArr = [];
        while( ($filename = readdir($handler)) !== false ) {
            if($filename != "." && $filename != ".." && $filename != "qrcode.png"){
                $imgArr[$count] = $filename;
                ++$count;
            }
        }

        // 获取所有已经分享的图片
        $share_model = M('activity_img_share');
        $result =   $share_model->field('imgurl')->select();
        $n_r = [];
        foreach ($result as $k => $v) {
            $n_r[$k] = substr($v['imgurl'],5);
        }

        $count = 0;
        foreach ($imgArr as $v) {
            if (!in_array($v,$n_r)) {
                // 删除
                unlink('./tmp/'.$v);
                $count ++ ;
            }
        }

        dump("本次共清理了".$count."个临时图片");
    }
posted @ 2018-04-14 17:39  TBHacker  阅读(282)  评论(0编辑  收藏  举报