0

特定のフォルダから特定の拡張子のファイルを削除するこのスクリプトを見つけました。拡張子の代わりに末尾が「-75x60.jpg」のファイルを検索する方法を知りたいのですが、助けてください。tx

ファイル: 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

ファイル: test.php

require_once "file_del.class.php";

$path = "/your/desired/path/to/delete_from";
$ext  = "desired_ext";

$delObj = new fileDel($ext);

$delObj->delDirFiles($path);
4

3 に答える 3

1

これを行うのにクラスは本当に必要ないと思います。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ため、テストしている文字列と同じ文字数が返されます。次に、2つが等しいかどうかを確認できます。

于 2012-04-10T14:48:59.357 に答える
1

標準の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;
    }
}
于 2012-04-10T15:36:54.177 に答える
0

これを試して:

<?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);
?>

それが役に立てば幸い。

于 2012-04-10T14:53:09.933 に答える