2012-04-10 6 views
0

特定のフォルダから特定の拡張子のファイルを削除するこのスクリプトが見つかりました。拡張子ではなく末尾にあるファイルを検索するにはどうすればよいですか?-75x60 .jpg "私を助けてください。 TX拡張子の代わりに "end with"を検索する

ファイル: 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); 
+0

がhttp://php.net/glob – YMMD

+0

:-)ただ、先端を見てください:そのスクリプトを使用しないでください、非常に壊れやすい外観と、これはファイルの削除についてですと、私は言うだろう:cで処理するあなたは足に自分を撃ってはいません。 – hakre

答えて

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

はそれがお役に立てば幸いです。

1

私はあなたが本当にこれを行うためのクラスが必要とは思いません。文字列(例えばファイル名)が何かを終了した場合、それは同じ数のを返しますので、あなたは、substrに負のオフセットを渡すことができます検出し、とにかく

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

:RecursiveIteratorIteratorとRecursiveDirectoryIteratorを使用して、これは些細な挑戦であります文字列をテスト対象の文字列として返します。次に、2つが等しいかどうかを確認できます。

+0

それは働いた! tx manちょうど私が必要なもの – Ered

1

簡単FilterIterator組み合わせる標準PHP再帰的なディレクトリイテレータを利用した別の変形は、:

<?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; 
    } 
} 
+0

あなたは、そのコードを複数回使用する必要がある場合に備えて、これはすばらしい追加です。 –

+0

@BerryLangerak:はい、またはイテレータから配列などです。 – hakre

関連する問題