搜索“结尾为”而不是扩展名


Search "end with" instead of extension

我发现这个脚本可以从特定文件夹中的特定扩展名中删除文件,我想知道如何搜索以"-75x60.jpg"结尾而不是扩展名的文件,请帮助我。 德克萨斯州

文件: file_del.class.php

class fileDel
{
     var $extension;
   /** 
   *   @purpose: Sets path and extension
   *   @params : path, file extension to delete
   *   @return none
   */
   function fileDel($extension)
   {
      $this->extension = $extension;    
   }//End of function
   /** 
   *   @purpose: Recursively deleting files
   *   @params : path to execute
   *   @return : none
   */
   function delDirFiles ($path)
   {
      $dir = opendir ($path);
      while ($file = readdir ($dir)) 
      {
         if (($file == ".") or ($file == ".."))
         {
            continue;
         }                
             if (filetype ("$path/$file") == "dir")
             {              
            $this->delDirFiles("$path/$file");
         }                
         //whether file of desired extension is found
                 elseif($this->findExtension($file)==$this->extension)
                 {                    
            if(@unlink ("$path/$file"))
            {
               echo "$path/$file >> <b>Deleted</b><br>";
            }   
         }                   
      } //End of while
      closedir($dir);
   }//End of function
   /** 
   *   @purpose: Finding extension of a file
   *   @params : filename
   *   @return : extension
   */
   function findExtension($file)
   {
      return array_pop(explode(".",$file));
   }//End of function
} //End of class

文件:测试.php

require_once "file_del.class.php";
$path = "/your/desired/path/to/delete_from";
$ext  = "desired_ext";
$delObj = new fileDel($ext);
$delObj->delDirFiles($path);

我认为你真的不需要一个类来做到这一点。使用RecursiveIteratorIterator和RecursiveDirectoryIterator,这是一个微不足道的挑战:

<?php
$endswith = '-75x60.jpg';
$directory = './tmp';
$it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $directory ) );
$endslength = strlen( $endswith );
foreach( $it as $file ) {
    if( substr( $file, -( $endslength ) ) === $endswith ) {
        echo "Removing $file.'n";
        unlink( $file );
    }
}

无论如何,检测字符串(例如文件名)是否以某物结尾,您可以将负偏移量传递到substr,因此它将返回与您正在测试的字符串相同数量的字符。然后,您可以检查两者是否相等。

另一个使用标准PHP递归目录迭代器的变体,结合一个简单的FilterIterator

<?php
foreach (new FileEndingLister('/path/to/dir', '-75x60.jpg') as $file)
{
    unlink($file);
}

FileEndingLister是几行代码,实例化递归目录迭代器并根据每个文件名的末尾提供过滤器:

class FileEndingLister extends FilterIterator
{
    private $ending;
    public function __construct($path, $ending) {
        $this->ending = $ending;
        parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
    }
    public function accept() {
        return $this->isFile() 
          && substr(parent::current()->getFilename(), -strlen($this->ending)) === $this->ending;
    }
}

试试这个:

<?php
class fileDel {
    var $fileEndsWith;
    function fileDel($fileEndsWith) {
        $this->fileEndsWith = $fileEndsWith;
    }
    function delDirFiles ($path) {
        $dir = opendir ($path);
        while ($file = readdir ($dir)) {
            if (($file == ".") or ($file == "..")) {
                continue;
            }
            if (filetype("$path/$file") == "dir") {
                $this->delDirFiles("$path/$file");
            }
            elseif($this->findFileEndsWith($file)==$this->fileEndsWith) {                    
                if(@unlink ("$path/$file")) {
                    echo "$path/$file >> <b>Deleted</b><br>";
                }
            }
        }
        closedir($dir);
    }
    function findFileEndsWith($file) {
        $length = strlen( $this->fileEndsWith );
        return substr( $file, -$length, $length );
    }
}
require_once "file_del.class.php";
$path = "/your/desired/path/to/delete_from";
$ext  = "desired_file_end_str";
$delObj = new fileDel($ext);
$delObj->delDirFiles($path);
?>

希望对您有所帮助。