1

如何使用 php 只保留特定文件并删除目录中的其他文件?

例子:
1/1.png, 1/2.jpeg, 1/5.png ...

文件编号和文件类型是随机的,如 x.png 或 x.jpeg,但我有一个2.jpeg文件需要保留的字符串。

任何建议如何做到这一点?

感谢您的回复,现在我像下面这样编码,但取消链接功能似乎不起作用删除任何内容..我需要更改一些设置吗?我正在使用 Mamp

更新

// explode string <img src="u_img_p/5/x.png">
$content_p_img_arr = explode('u_img_p/', $content_p_img);
$content_p_img_arr_1 = explode('"', $content_p_img_arr[1]);    // get 5/2.png">
$content_p_img_arr_2 = explode('/', $content_p_img_arr_1[0]);    // get 5/2.png
print $content_p_img_arr_2[1];    // get 2.png   < the file need to keep

$dir = "u_img_p/".$id;  
if ($opendir = opendir($dir)){
    print $dir;
    while(($file = readdir($opendir))!= FALSE )
        if($file!="." && $file!= ".." && $file!= $content_p_img_arr_2[1]){
            unlink($file);
            print "unlink";
            print $file;
        }
    }
} 

我将代码取消链接路径更改为文件夹,然后它就可以了!!

 unlink("u_img_p/".$id.'/'.$file);  
4

4 回答 4

2

http://php.net/manual/en/function.scandir.php

这会将目录中的所有文件放入一个数组中,然后您可以在数组上运行 foreach() 并在每个文件上查找模式/匹配项。

unlink() 可用于删除文件。

$dir = "/pathto/files/"
$exclude[] = "2.jpeg";
foreach(scandir($dir) as $file) {
 if (!in_array($file, $exclude)) {
  unlink("$dir/$file");
 }
}

简单明了。您可以将多个文件添加到$exclude阵列中。

于 2013-07-25T03:08:32.403 回答
0
 $dir = "your_folder_path";  
 if ($opendir = opendir($dir)){
    //read directory
     while(($file = readdir($opendir))!= FALSE ){
      if($file!="." && $file!= ".." && $file!= "2.jpg"){
       unlink($file);
      }
     }
   } 
于 2013-07-25T03:09:29.320 回答
0
function remove_files( $folder_path , $aexcludefiles )
{
    if (is_dir($folder_path))
    {
        if ($dh = opendir($folder_path))
        {
            while (($file = readdir($dh)) !== false)
            {
                if( $file == '.' || $file == '..' )
                    continue ;

                if( in_array( $file , $aexcludefiles ) )
                    continue ;

                $file_path = $folder_path."/".$file ;
                if( is_link( $file_path ) )
                    continue ;

                unlink( $file_path ) ;
            }

            closedir($dh);
        }
    }
}


$aexcludefiles = array( "2.jpeg" )
remove_files( "1" , $aexcludefiles ) ;
于 2013-07-25T03:13:07.633 回答
0

我很惊讶人们不再使用glob()。这是另一个想法:

$dir = '/absolute/path/to/u_img_p/5/';
$exclude[] = $dir . 'u_img_p/5/2.jpg';

$filesToDelete = array_diff(glob($dir . '*.jpg'), $exclude);
array_map('unlink', $filesToDelete);

首先,glob()根据提供给它的模式返回一个文件数组。接下来,array_diff()查找第一个数组中不在第二个数组中的所有元素。最后,使用array_map()withunlink()删除除排除文件之外的所有文件。一定要使用绝对路径*

你甚至可以把它变成一个辅助函数。这是一个开始:

<?php
    /**
     * @param  string $path
     * @param  string $pattern
     * @param  array $exclude
     * @return bool
     */
    function deleteFiles($path, $pattern, $exclude = [])
    {
        $basePath = '/absolute/path/to/your/webroot/or/images/or/whatever/';
        $path = $basePath . trim($path, '/');
        if (is_dir($path)) { 
            array_map(
                'unlink', 
                array_diff(glob($path . '/' . $pattern, $exclude)
            );

            return true;
        }

        return false;
    }
  • unlink()glob()除非返回的路径数组恰好与调用位置相关,否则将不起作用unlink()。由于glob()只会返回匹配的内容,因此最好使用包含要删除/排除的文件的目录的绝对路径。请参阅文档和有关glob()匹配方式的评论,并试一试以了解其工作原理。
于 2018-10-05T20:14:49.657 回答