2012-04-10 46 views
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); 
+0

看一看http://php.net/glob :-) – YMMD 2012-04-10 14:40:37

+0

只是一個提示:不要使用該腳本,看起來非常脆弱,因爲這是關於刪除文件,我會說:用c處理是,不要把自己打到腳上。 – hakre 2012-04-10 14:42:00

回答

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

我不認爲你真的需要一個類來做到這一點。使用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,所以它會返回相同數量的字符作爲你正在測試的字符串。然後,你可以檢查兩者是否相等。

+0

它的工作! tx男人正是我所需要的 – Ered 2012-04-10 14:52:51

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

這是一個很好的補充,以防您需要多次使用該代碼。 – 2012-04-11 07:02:46

+0

@BerryLangerak:是的,或者用於數組等的迭代器。 – hakre 2012-04-11 08:51:49