删除除运行删除代码的文件外的所有文件 [英] deleting all files in except the one running the delete code

查看:113
本文介绍了删除除运行删除代码的文件外的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过单个命令删除我的域的public_html目录中的所有文件。对于这个一个SO的问题有这个答案:

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:

<?php
array_map('unlink', glob("path/to/temp/*"));
?>

我如何添加例外,也许使用这些代码或其他东西?我不想删除运行删除代码的文件,因为这个文件也将在public_html文件夹中。

How can i add exceptions to this, maybe using this code or something else? I don't want to delete file(s) running the delete code as this file too will be in the public_html folder.

推荐答案


阅读类结构,您将获得一些有用的功能

read the class structure, you will get some useful features



<?php
// this will call your given callback with the file or folder informations, so you can use your logic to delete/copy/move the file/folder
// note: this is a recursive function
$literator  = new ipDirLiterator(
  "your/path/to/root/or/anyfolder/", // for getting root path, you can use $_SERVER["DOCUMENT_ROOT"]
  array(
    "file"  =>  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 );
      }
    }
  }
}
?>

详细示例:

<?php
/**
 * It will recursively go through the all directories and files under the directory you have given.
 * Here we are going to delete all the files order than 1 hour
**/
/**
 * Path to the folder you want to process
**/
$basepath = "any/path/to/files/or/folder/";
/**
 * Callbacks
**/
$file_callback  = function( $file /* information of current file in the loop */ ) { // your callback for files
  $modified_time  = filemtime( $file["pathname"] );
  $current_time   = time();
  $time_differnce = ( $current_time - $modified_time );
  if ( $time_differnce > 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();
?>

这篇关于删除除运行删除代码的文件外的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆