deleting all files in except the one running the delete code

后端 未结 2 800
一生所求
一生所求 2021-01-26 03:51

I want to delete all the files in the public_html directory of my domain by a single command. For this one of SO\'s questions had this answer:



        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-26 04:25

    read the class structure, you will get some useful features

      function( $file ) { // your callback to delete files
          if ( basename( $file["pathname"] ) !== basename( __FILE__ ) ) {
            unlink( $file["pathname"] );
          }
        }
      ),
      true
    );
    
    class ipDirLiterator {
      protected $basepath   = false;
      protected $callbacks  = false;
    
      protected $checked    = array();
    
      public function __construct( $basepath = false, $callbacks = false, $init = false ) {
        $this->basepath   = ( $basepath ) ? realpath( $basepath ) : false;
        $this->callbacks  = $callbacks;
        if ( $init ) {
          $this->literate();
        }
      }
    
      public function literate( $dir = false ) {
        if ( !$this->basepath ) {
          return false;
        }
        if ( $dir === $this->basepath ) {
          return false;
        }
        if ( !$dir ) {
          $dir  = $this->basepath;
        }
    
        $dir  = realpath( $dir );
    
        if ( strstr( $dir, basename( $this->basepath ) ) === false ) {
          return false;
        }
        if ( in_array( $dir, $this->checked ) ) {
          return false;
        }
    
        $this->checked[]  = $dir;
        $items  = new DirectoryIterator( $dir );
    
        foreach( $items as $item ) {
          if ( $item->isDot() ) {
            if ( $item->getFilename() === ".." ) {
              $this->literate( dirname( $item->getPath() ) );
            }
            $this->callback( "dot", $this->info( $item ) );
            continue;
          }
          if ( $item->isFile() || $item->isLink() ) {
            $this->callback( "file", $this->info( $item ) );
          }
          if ( $item->isDir() && !$item->isLink() ) {
            $this->literate( $item->getPathname() );
            $this->callback( "dir", $this->info( $item ) );
          }
        }
      }
    
      private function info( $item ) {
        $info = array(
          "filename"  =>  $item->getFilename(),
          "extension" =>  pathinfo( $item->getFilename(), PATHINFO_EXTENSION ),
          "pathname"  =>  $item->getPathname(),
          "path"  =>  $item->getPath(),
          "readable"  =>  (bool)$item->isReadable(),
          "writable" =>  (bool)$item->isWritable(),
          "executable"  =>  (bool)$item->isExecutable(),
          "created_on" =>  $item->getCTime(),
          "last access" =>  $item->getATime(),
          "modified_on" =>  $item->getMTime(),
          "inode" =>  $item->getInode(),
          "permissions" =>  $item->getPerms(),
          "is_dir" =>  (bool)$item->isDir(),
          "is_dot" =>  (bool)$item->isDot(),
          "type"  =>  $item->getType(),
          "group" =>  $item->getGroup(),
          "owner" =>  $item->getOwner(),
          "size"  =>  $item->getSize()
        );
        return $info;
      }
    
      private function callback( $callback = "file", $args = null ) {
        if ( $this->callbacks ) {
          if ( isset( $this->callbacks[$callback] ) ) {
            call_user_func( $this->callbacks[$callback], $args );
          }
        }
      }
    }
    ?>
    

    Detailed example:

     3600 ) {
        unlink( $file["pathname"] );
      }
    };
    $folder_callback  = function( $file /* information of current folder in the loop */ ) { // your callback for folders
      // proceess your folder here
    };
    /**
     * Initialize the class
    **/
    $literator  = new ipDirLiterator( $basepath, array( "file" => $file_callback, "dir" => $folder_callback ) );
    $literator->literate();
    ?>
    

提交回复
热议问题