我发现这个脚本从特定文件夹中删除特定文件夹中的文件我想知道如何才能搜索以文件结尾而不是扩展名的文件让我们说“-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 :(得分: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
,因此它将返回与您正在测试的字符串相同数量的字符。然后,您可以检查两者是否相等。
答案 1 :(得分: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;
}
}
答案 2 :(得分: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);
?>
希望它有所帮助。