php文件,用于删除带有特定字符串的所有图像

php文件,用于删除带有特定字符串的所有图像,php,wordpress,Php,Wordpress,我需要一个php脚本,可以删除一个文件夹和子文件夹中的所有文件,而不是包含一个“-”。该文件是图像-专门复制wordpress媒体库中的图像缩略图 谢谢 必须使用unlink()删除文件,并使用strpos()检查文件名中是否存在- $dup_thumb = glob('path to folder'); foreach($dup_thumb as $duplicate){ if(is_file($duplicate) && strpos($duplicate,'-') !==

我需要一个php脚本,可以删除一个文件夹和子文件夹中的所有文件,而不是包含一个“-”。该文件是图像-专门复制wordpress媒体库中的图像缩略图


谢谢

必须使用
unlink()
删除文件,并使用
strpos()
检查文件名中是否存在
-

$dup_thumb = glob('path to folder');
foreach($dup_thumb as $duplicate){ 
if(is_file($duplicate) && strpos($duplicate,'-') !== false)
     unlink($duplicate);
}
让结构更简单

/images
    - /list1
        image-11.jpeg
        image-12.jpeg
    - /list2
        image-21.jpeg
        image-22.jpeg
使用递归DirectoryIterator,我们可以删除所有图像

    $parentDirPath = "images";//pathtofolder
    if (is_dir($parentDirPath)) {
        $directory = new RecursiveDirectoryIterator($parentDirPath);
        foreach (new RecursiveIteratorIterator($directory) as $fileNetPath => $file) {
            if ($file->isFile()) {
                $fileName = $file->getFilename();
                if (preg_match("/-/", $fileName)) {//checking for matching names
                    echo "<br/> Deleting file : ".$fileNetPath;
                    @unlink($fileNetPath);                
                }
            }
        }
    }
$parentDirPath=“images”//pathtofolder
if(is_dir($parentDirPath)){
$directory=new recursivedirectoryinterator($parentDirPath);
foreach(新的递归迭代器($directory)为$fileNetPath=>$file){
如果($file->isFile()){
$fileName=$file->getFilename();
if(preg_match(“/-/”,$fileName)){//检查匹配的名称
echo“
删除文件:.$fileNetPath; @取消链接($fileNetPath); } } } }
希望这对您有所帮助


<?php
$dir = "img";//folder url which consist file to delete. you can use     wordpress file location using wordpress function 
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
  if (strpos($filename,'_') !== false)//check whether it conist of '_'
  {
   unlink('img/'.$filename);//remove file from folder
  }   
}
?>

您为此尝试过什么吗?它会只删除我放入代码中的路径中的文件吗?或者它也将从根目录中删除所有文件?仅删除指定的路径。我将如何在我的网站上运行此文件?我将如何实际联机运行此文件?在本地创建一个与您的文件夹结构相同的示例,然后尝试此脚本。确保它按照您的要求工作。然后尝试使用原始文件夹。为$PARENTDIRPATH提供文件夹的完整路径我是否需要特殊命令来运行它,还是只需将文件上载到images文件夹并通过链接访问该文件?在删除文件时,请确保它没有删除任何需要的项目。您可以通过注释删除并显示文件名{echo$fileNetPath.“
”;//@unlink($fileNetPath);}来上载php脚本。如果工作正常,请取消注释删除并再次运行脚本。Cellent能够使其工作。。如何使其显示结果,例如删除文件。。谢谢