zoukankan      html  css  js  c++  java
  • PHP 删除网站上传没用的图片/文件,删除CMS数据库不存在的图片,清理服务器空间

    本代码是根据行云海CMS来开发的,可以根据自己情况二次开发,

    主要用于清理网站上传没用的图片(在数据库里面的不会被删)

    代码如下:

    <?php
    /**
                 ____________________________________________________
                /                                                    
               |    _____________________________________________     |
               |   |                                             |    |
               |   |  C:> _                                     |    |
               |   |                                             |    |
               |   |                                             |    |
               |   |                                             |    |
               |   |                                             |    |
               |   |          XiaoHe    QQ496631085              |    |
               |   |                                             |    |
               |   |                                             |    |
               |   |                                             |    |
               |   |                                             |    |
               |   |                                             |    |
               |   |                                             |    |
               |   |_____________________________________________|    |
               |                                                      |
                \_____________________________________________________/
                       \_______________________________________/
                    _______________________________________________
                 _-'    .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.  --- `-_
              _-'.-.-. .---.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.--.  .-.-.`-_
           _-'.-.-.-. .---.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-`__`. .-.-.-.`-_
        _-'.-.-.-.-. .-----.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-----. .-.-.-.-.`-_
     _-'.-.-.-.-.-. .---.-. .-----------------------------. .-.---. .---.-.-.-.`-_
    :-----------------------------------------------------------------------------:
     * 清理文件ClearController
     * @Author: XiaoHe QQ496631085   VX:he4966
     * @Date:   2020-06-15 10:00:00
     * @Last Modified by:   gosea
     * @Last Modified time: 2020-06-16 16:00:00
     * 使用说明 ,本代码是放到行云海/App/Api/Controller/下面,清理文件,访问地址:  你的域名/api/clear/index
     * 手动导出你的数据库 改名文件或者$db的文件名,其余的参数自己配置一下
     */
    
    namespace ApiController;
    use ThinkController;
    
    class ClearController extends Controller {
    
    
    
        private $db = 'db.txt';           //需要被找的数据库一定要最新导出(手动放到根目录) 然后改名[[必改]]
        private $houzui = '.jpg|.png|.jpeg|.gif'; //需要被找的文件后缀 可以是pdf doc zip等    [[根据自己的情况修改]]
        private $delpath = "/uploads/img1";//                  //扫描的要删除的目录文件夹    [[根据自己的情况修改]]
    
        private $imgpaths=array();//图片img数组列表 以方便删除
        private $imgpaths_cunzai=array();//存在 的图片
        private $imgpaths_nocunzai=array();//不存在的图片
    
    
        private $delFileNames = 'deleteimgs.txt';
        private $mulu = 'mulu.txt';//文件夹目录
     
        private $stime;
    
        public function __construct()
        {
            $this->stime = time();//累计时间
        }
    
        public function __destruct(){
          echo "<br><H1>本次总耗时间".(time()-$this->stime)."</H1>"; //结束时间 
        }
    
        //自己封装的函数 方便调试
        function p($arr,$zhu=null){
          if(is_array($arr)){
            var_dump($arr);
          }else{
            echo "<br>".$zhu.$arr."<br>";
          }
        }
    
    
    
    
    
        public function index(){
          header('Content-type:text/html;charset=utf-8');
          echo '<ul class="breadcrumbs-custom-path">
                  <li><a target="_blank" href="/api/clear/start">开始一键搜索扫描</a></li><br>
                  <li><a target="_blank" href="/api/clear/scanimgdir">1扫描目录并保存</a></li><br>
                  <li><a target="_blank" href="/'.$this->mulu.'">1-1查看扫描的目录</a></li><br>
    
                  <li><a target="_blank" href="/api/clear/sanfile">1扫描文件并保存_需要扫描的文件后缀名:'.$this->houzui.'</a></li><br> 
                  <li><a href="/'.$this->db.'">查看数据库</a></li><br>
                </ul>';
        }
        //扫描目录 并保存
        public function scanimgdir(){
          $run_path = $_SERVER['DOCUMENT_ROOT'].$this->delpath;//运行目录
          $scandir = $this->sandir($run_path);
          $this->xieConfig($this->mulu,$scandir);
        }
    
        //扫描文件 并找查
        public function scanfile(){
          $sandir = $this->readConfig($this->mulu);
    
        }
    
    
    
    
    
        //写配置文件(文件地址,写入的一维数组 )
        public function xieConfig($filepath,$arr){
          file_put_contents($filepath,implode($arr, ','));
        }
        //读一维数组配置
        public function readConfig($filepath){
          return explode(',',file_get_contents($filepath));
        }
    
    
        public function start(){
    
          $run_path = $_SERVER['DOCUMENT_ROOT'].$this->delpath;//运行目录
          $scandir = $this->sandir($run_path);
          $imgname = $this->scanimg($scandir);//文件名数组 以方便在数据库中查找 例如/qq496631085.png
          $content = file_get_contents($this->db);//获取数据内容 
    
          foreach ($imgname as $key => $file_name) {
            if(strpos($content,$file_name)>0){
              array_push($this->imgpaths_cunzai, $file_name);
              continue;
            }else{
              array_push($this->imgpaths_nocunzai, $file_name);
            }
          }
    
          p("QQ496631085  网站开发制作 微信小程序 后端开发************XiaoHe****************************************************************************************************************");
          echo "图片总共".sizeof($imgname)."张,不存在".sizeof($this->imgpaths_nocunzai)."............存在".sizeof($this->imgpaths_cunzai);
    
          p("不存在的图片:".sizeof($this->imgpaths_nocunzai)."张");
    
          p($this->imgpaths_nocunzai);
    
          file_put_contents($this->delFileNames,implode($this->imgpaths_nocunzai, ','));
    
         
    
          echo "已删除".$ci."张图片";
          if($this->imgpaths_nocunzai>0){
            echo '<a href="/'.$this->delFileNames.'">查看待删除的所有图片</a>';
            echo '<br><a href="/Api/Clear/deleteimgs">点击删除所有图片</a>';
          }else{
            p("没有可以删的图片了");
          }
          
        }
    
        //扫描目录
        public function sandir($path){
             $data  = scandir($path);
            foreach ($data as $key => $value) {
              // 判断最后一位是否小数点
              if(strpos($value,'.')>0  || $value=="." || $value==".."){
                unset($data[$key]);//删除有带.的目录(如.   ..   )
              }else{
                $data[$key] = $path.'/'.$value;
              }
    
            }
    
          return $data;
        }
    
    
        //扫描文件图片文件 
        /*
        如果是图片就加入到$this->imgpaths数组 并把文件名加入到 $imgdata数组中
        返回$imgdata
        */
        public function scanimg($patharr){
            $imgdata = array();
            foreach ($patharr as $key => $path) {
    
                $filesnames = scandir($path);
                 // p($filesnames);
                 foreach ($filesnames as $key => $filename){
                   //判断是否是图片
                  // echo $filename;
                  if(strlen($filename)>4){
                    $houzui = substr($filename,-4);
    
                    if(strstr($this->houzui,$houzui)){
                      // echo  $path.'/'.$filename.'<br>';
                      array_push($this->imgpaths, $path.'/'.$filename);
                      array_push($imgdata, '/'.$filename);
                    }
                  }
                    // array_push($imgdata,$value)             
              }                  
            }
            file_put_contents('paths.txt', implode($this->imgpaths, ','));
            return $imgdata;
        }
    
    
        // public function getPaths($run_path=""){
        //   if(file_exists('paths.txt')){
        //     $pathArr = explode(',',file_get_contents('paths.txt'));
        //     $this->imgpaths = $pathArr;
        //   }else{
        //     $scandir = $this->sandir($run_path);
        //     $imgname = $this->scanimg($scandir);
        //     // return $imgname;
        //   }
        // }
    
        public function deleteimgs(){
    
    
          $ci=0;
          $run_path = $_SERVER['DOCUMENT_ROOT'].$this->delpath;//运行目录
          $scandir = $this->sandir($run_path);
          $imgname = $this->scanimg($scandir);
          // $this->getPaths($run_path);
    
         
          $txt = file_get_contents($this->delFileNames);
          $this->imgpaths_nocunzai = explode(',',$txt);
          p($this->imgpaths_nocunzai);
          // exit;
          //字符到数组
             foreach ($this->imgpaths_nocunzai as $key => $path) {
            //查询具体路径.
            echo "<br>删除文件ing:".$path;
            foreach ($this->imgpaths as $rkey => $rootpath) {
    
              // if(strpos($rootpath,$path)){
                if(substr_count($rootpath,$path)>=1){
    
                if(unlink($rootpath)){//删除文件
                   echo $roorpath."已删除<br>";
                   $ci++;
                 }
                
              }
            }
            
          }
          echo "共删除".$ci."张";
        }
    
        public function _empty() {
            header("HTTP/1.1 404 Not Found");
            header("Status: 404 Not Found");
            // header("Content-Type: text/html; charset=utf-8");
            // $this->display('Public:404');
            echo '404 Not Found';
    
        }
    
    
    
    }
  • 相关阅读:
    python—Celery异步分布式
    python 安装虚拟环境步骤
    同源策略:
    git 的用法和命令
    一分钟搞定AlloyTouch图片轮播
    JavaScript学习总结(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
    CSS3与页面布局学习总结(八)——浏览器兼容与前端性能优化
    移动前端UI选择
    你必须收藏的Github技巧
    HTML5 学习笔记(二)——HTML5新增属性与表单元素
  • 原文地址:https://www.cnblogs.com/xiaohe520/p/13141555.html
Copyright © 2011-2022 走看看