2012-04-10 2 views
0

특정 폴더에서 특정 확장명의 파일을 삭제하는이 스크립트를 발견했습니다. 어떻게 확장명 대신 끝나는 파일을 검색 할 수 있는지 알고 싶습니다. "-75x60 .jpg "제발 도와주세요. 텍사스확장명 대신 "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 사용이 사소한 도전 문자를 테스트 할 문자열로 지정하십시오. 그런 다음 둘이 같은지 확인할 수 있습니다.

+0

그것이 효과가있다! tx man just i need – 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