读取文件

<?php


class lib_img
{ 
/** 
* 图片保存下来
* @param $cssUrl css的url地址
* @param $dir 保存图片的目录
* @return void
*/
    static public function  add_img_chil($opt){
        $ret = db('main')->insert($opt,tbl('imge'));
    } 
    static public function saveimage()
    { 
         $dir = "../css";  //要获取的目录
         //先判断指定的路径是不是一个文件夹
         if (is_dir($dir)){
          if ($dh = opendir($dir)){
           while (($file = readdir($dh))!= false){
            //文件名的全路径 包含文件名
            $filePath = $dir.'/'.$file;
            //获取文件修改时间
                if (is_dir($filePath)){
                  if ($dh1 = opendir($filePath)){
                   while (($file1 = readdir($dh1))!= false){
                    //文件名的全路径 包含文件名
                    $filePath1 = $filePath.'/'.$file1;
                    //获取文件

                   $sub=substr(strrchr($filePath1, '.'), 1);
                   if($sub=='css'){
                         //note 取得样式文件内容
                        $styleFileContent = $filePath1;

                        /****************************/
                        $content = file_get_contents($styleFileContent); 
                                $patterns = "/url\((.*).([^.]+)\.(?:jpg|gif|png|bmp)\)/"; //正则根据不同地址需要变换
                                $ing=preg_match_all($patterns, $content,$matches);
                                $imagesUrls = array_unique($matches[0]);
                                if(count($imagesUrls) > 0){
                                         foreach((array)$imagesUrls as $key=>$image)
                                        { 
                                                       $patterns1 = "/\/(image).(.*).([^.]+)\.(?:jpg|gif|png|bmp)/";
                                                       $img1=preg_match_all($patterns1,$image,$mat);
                                                       $imagesUrl = array_unique($mat['0']);
                                                       foreach ($imagesUrl as  $val) {
                                                           $img_path[] = $val;
                                                       }
                                                       $str = ltrim($image,'url(');
                                                       $str1 = rtrim($str,')');
                                                       $str2=substr($str1,strrpos($str1,'/'));
                                                       $img_name[]=ltrim($str2,"/");
                                        }
                              }

                        }

                   }//while  end
                  
                  }
                }
           }
           closedir($dh);
          }
         }
         $img =array();
         foreach ($img_path as $key => $value) {
            $img[$key]['img_path']=$img_path[$key];
         }
         foreach ($img_name as $key => $value) {
             $img[$key]['img_name']=$value;
         }
         //print_r($img);
         /*插入数据库中*/
         for ($i=0; $i <count($img) ; $i++) { 
              $ret = db()->insert($img[$i],tbl('imge'));
         }
        
    }
    //封装函数递归函数查询所有的图片
    //$path = "../image";
    static public function add_img_tal1($path){
            if(is_dir($path))
            {

                if($current_dir = opendir($path)){

                 //opendir()返回一个目录句柄,失败返回false
                    while(($file = readdir($current_dir)) !== false) {    
                    //readdir()返回打开目录句柄中的一个条目
                       $sub_dir = $path .'/'. $file;    //构建子目录路径
                        if($file == '.' || $file == '..') {
                            continue;
                        } else if(is_dir($sub_dir)) {    //如果是目录,进行递归
                             $arr[$file]=self::add_img_tal1($sub_dir);
                        } else {    //如果是文件,直接输出

                            $str4 = ltrim($sub_dir,"\.\.");
                            $arr[]=$str4;
                        }
                    }
                    closedir($current_dir);
                    return $arr; 
                }
            }
    }
    
    static public function add_img_tal(){

                $path = "../image";
                $rest=self::add_img_tal1($path);
                // echo "<pre>";
                // print_r($rest);
                // echo "</pre>";
                foreach($rest as $key => $value){
                    if(is_array($value)){
                        foreach ($value as $ke => $val) {
                            if(is_array($val)){
                                foreach ($val as $k => $v) {
                                    if(is_array($v)){
                                        foreach ($v as $ks => $vs) {
                                            if(is_array($vs)){
                                                foreach ($vs as $ka => $va) {
                                                    $image[]=$va;
                                                }
                                            }else{
                                            $image[]=$vs;
                                            } 
                                        }
                                    }else{
                                    $image[]=$v;
                                    } 
                                }
                            }else{
                            $image[]=$val;
                            }
                        }
                    }else{
                        $image[]=$value;
                    }
                }
                // echo "<pre>";
                // print_r($image);
                // echo "</pre>";
                //exit;
                $sql = "select img_path from ".tbl('imge')."";
                $rst = db()->query_result($sql);
                foreach ($rst as $key => $value) {
                    foreach ($value as $k => $val) {
                        $arr1[]=$val;
                    }
                }
                foreach ($image as $key => $value) {
                    if(in_array($value, $arr1)){
                        $use[]['img_path']= $value;
                    }else{
                        $no_use[]['img_path']= $value;
                    }
                }
                // // print_r($use);
                // // print_r($no_use);
                //  /*插入数据库中*/
                 for ($i=0; $i <count($use) ; $i++) { 
                      $ret = db()->insert($use[$i],tbl('use_image'));
                 }
                 
                 for ($i=0; $i <count($no_use) ; $i++) { 
                      $ret = db()->insert($no_use[$i],tbl('no_use_image'));
                 }
    } 
   
    static public function list_use_image(){
        $sql = "select * from ".tbl('use_image')."";
        $rst = db()->query_result($sql);
        return $rst;
    }
    static public function list_no_use_image(){
        $sql = "select * from ".tbl('no_use_image')."";
        $rst = db()->query_result($sql);
        return $rst;
    }
    //js
    static public function saveimage_js($dir)
    { 
         if (is_dir($dir)){
          if ($dh = opendir($dir)){
           while (($file = readdir($dh))!= false){
            //文件名的全路径 包含文件名
            $filePath = $dir.'/'.$file;
            //获取文件修改时间
                if (is_dir($filePath)){
                  if ($dh1 = opendir($filePath)){
                   while (($file1 = readdir($dh1))!= false){
                    //文件名的全路径 包含文件名
                     $filePath1 = $filePath.'/'.$file1;
                    //获取文件

                   $sub=substr(strrchr($filePath1, '.'), 1);
                   if($sub=='js'){
                         //note 取得样式文件内容
                        $styleFileContent = $filePath1;

                        /****************************/
                        $content = file_get_contents($styleFileContent); 
                                $patterns = "/\/(image).(.*).([^.]+)\.(?:jpg|gif|png|bmp)/"; //正则根据不同地址需要变换
                                $ing=preg_match_all($patterns, $content,$matches);
                                $imagesUrls = array_unique($matches[0]);
                               //print_r($imagesUrls);
                                if(count($imagesUrls) > 0){
                                        foreach((array)$imagesUrls as $key=>$image)
                                        { 
                                          $img_path[] = $image;
                                         }
                              }

                        }

                   }//while  end
                  
                  }
                }
           }
           closedir($dh);
          }
         }
         // $img =array();
         // foreach ($img_path as $key => $value) {
         //    $img[$key]['img_path']=$img_path[$key];
         // }
         // foreach ($img_name as $key => $value) {
         //     $img[$key]['img_name']=$value;
         // }
         // //print_r($img);
         // /*插入数据库中*/
         // for ($i=0; $i <count($img) ; $i++) { 
         //      $ret = db()->insert($img[$i],tbl('imge'));
         // }
         //print_r($img_path);
         return $img_path;
        
    }
    //html
    static public function saveimage_html($dir)
    { 
         if (is_dir($dir)){
          if ($dh = opendir($dir)){
           while (($file = readdir($dh))!= false){
            //文件名的全路径 包含文件名
            $filePath = $dir.'/'.$file;
            //获取文件修改时间
                if (is_dir($filePath)){
                  if ($dh1 = opendir($filePath)){
                   while (($file1 = readdir($dh1))!= false){
                    //文件名的全路径 包含文件名
                     $filePath1 = $filePath.'/'.$file1;
                    //获取文件

                   $sub=substr(strrchr($filePath1, '.'), 1);
                   if($sub=='html'){
                         //note 取得样式文件内容
                        $styleFileContent = $filePath1;

                        /****************************/
                        $content = file_get_contents($styleFileContent); 
                                $patterns =  '/(image)\/.(.*?)\.(?:jpg|gif|png|bmp)/'; //正则根据不同地址需要变换
                                $ing=preg_match_all($patterns, $content,$matches);
                                $imagesUrls = array_unique($matches[0]);
                               //print_r($imagesUrls);
                                if(count($imagesUrls) > 0){
                                        foreach((array)$imagesUrls as $key=>$image)
                                        { 
                                          $img_path[] = "/".$image;
                                         }
                              }

                        }

                   }//while  end
                  
                  }
                }
           }
           closedir($dh);
          }
         }
         // $img =array();
         foreach ($img_path as $key => $value) {
            $img[$key]['img_path']=$img_path[$key];
         }
         // foreach ($img_name as $key => $value) {
         //     $img[$key]['img_name']=$value;
         // }
         // //print_r($img);
         // /*插入数据库中*/
         for ($i=0; $i <count($img) ; $i++) { 
              $ret = db()->insert($img[$i],tbl('imge'));
         }
         // echo "<pre>";
         // var_dump($img_path);
         // echo "</pre>";
         //return $img_path;
        
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值