Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php查找具有相似文件大小的文件并删除最旧的文件_Php_File_Glob - Fatal编程技术网

Php查找具有相似文件大小的文件并删除最旧的文件

Php查找具有相似文件大小的文件并删除最旧的文件,php,file,glob,Php,File,Glob,我正在尝试查找文件大小完全相同的文件,并使用最新的文件大小保留该文件,然后删除其他文件 到目前为止,我正在检查我所有的文件,并得到最新的更改文件 编辑了我的代码 当前代码删除了很多文件,但仍有3个文件大小相同,没有正确删除(保留最新更改的文件,删除其他文件) $files=glob($currentDir.\*.jpg',glob\u NOSORT); $count=计数($files); $itemsToRemove=[]; 对于($i=0;$i

我正在尝试查找文件大小完全相同的文件,并使用最新的文件大小保留该文件,然后删除其他文件

到目前为止,我正在检查我所有的文件,并得到最新的更改文件

编辑了我的代码 当前代码删除了很多文件,但仍有3个文件大小相同,没有正确删除(保留最新更改的文件,删除其他文件)

$files=glob($currentDir.\*.jpg',glob\u NOSORT);
$count=计数($files);
$itemsToRemove=[];
对于($i=0;$i<$count-1;$i++){
对于($j=$i+1;$j<$count;$j++){
//将$z[$i]与$z[$j]进行比较。。。
if(filesize($files[$i])==filesize($files[$j])){
如果(是_文件($files[$j])){
$itemsToRemove[]=$files[$j];
}
}
}
}
foreach($itemsToRemove作为$item){
如果(is_文件($item)){
取消链接(项目);
}
}

但结果是错误的。它仍然显示一些重复的文件,其大小与某些文件的大小完全相同。我认为我比较它们是错误的,我需要将每个值与数组中的所有其他值进行比较。

我认为您的任务可以解决如下问题:

$curSize = false;
foreach ($files as $file) {
    if ($curSize === false || filesize($file) !== $curSize) {
        $curSize = filesize($file);
    } else {
        unlink($file);
    }
}

我认为您的任务可以通过以下方式解决:

$curSize = false;
foreach ($files as $file) {
    if ($curSize === false || filesize($file) !== $curSize) {
        $curSize = filesize($file);
    } else {
        unlink($file);
    }
}

我不确定这是否是你想要的,但我希望你能从中得到一些帮助!:-)


我不确定这是否是你想要的,但我希望你能从中得到一些帮助!:-)


数组\u值($files)
不适用于reference@u_mulder如何“重置”它?我看不出每次迭代都要重新索引数组的原因。在迭代过程中取消设置元素也会导致循环出现奇怪的行为。@u\u mulder我想对其重新编制索引,因为我正在尝试获取最后一个更改文件,该文件位于数组的键0处。我不理解这里的逻辑。您的
$currentFile
永远不会更改,因此重新编制索引的目的是什么?
数组值($files)
不适用于reference@u_mulder如何“重置”它?我看不出每次迭代都要重新索引数组的原因。在迭代过程中取消设置元素也会导致循环出现奇怪的行为。@u\u mulder我想对其重新编制索引,因为我正在尝试获取最后一个更改文件,该文件位于数组的键0处。我不理解这里的逻辑。您的
$currentFile
从未更改,因此重新编制索引的目的是什么?似乎删除了“更多”,但并非所有这不会考虑修改时间?似乎删除了“更多”但并非所有这些都不会考虑修改时间?我需要在哪里取消链接/删除数据?@utdev-您可以将原始文件数组与使用文件数组进行比较。我已经更新了答案。我需要在哪里取消链接/删除数据?@utdev-您可以将原始文件数组与使用文件数组进行比较。我已经更新了答案。
<?php
$files = glob('*.jpg', GLOB_NOSORT);

//Get filetimes and only include unique filetimes (array_unique below)
$filetimes_arr = array_map('filemtime', $files);
array_multisort($filetimes_arr, SORT_NUMERIC, 
SORT_DESC, $files); //latest changed files 0,1...

//This would include only unique filetimes but still having same key as $files
$uniqe_times = array_unique($filetimes_arr); 

/*
example:

($files)
  0 => string 'brannebrona-e1536697041347-144x150.jpg' (length=38)
  1 => string 'brannebrona-e1536697041347-150x150.jpg' (length=38)
  2 => string 'brannebrona-e1536697041347-250x150.jpg' (length=38)
  3 => string 'brannebrona-e1536697041347-288x300.jpg' (length=38)
  4 => string 'brannebrona-e1536697041347-393x311.jpg' (length=38)
  5 => string 'brannebrona-e1536697041347-649x250.jpg' (length=38)
  6 => string 'brannebrona-e1536697041347-649x580.jpg' (length=38)
  7 => string 'brannebrona-e1536697041347.jpg' (length=30)
  8 => string 'brannebrona-e1536696975180.jpg' (length=30)
  9 => string 'brannebrona-e1536696959513.jpg' (length=30)
  10 => string 'brannebrona-1565x580.jpg' (length=24)
  11 => string 'brannebrona-250x150.jpg' (length=23)
  12 => string 'brannebrona-393x311.jpg' (length=23)
  13 => string 'brannebrona-1024x450.jpg' (length=24)
  14 => string 'brannebrona-1180x250.jpg' (length=24)
  15 => string 'brannebrona-150x150.jpg' (length=23)
  16 => string 'brannebrona-150x66.jpg' (length=22)
  17 => string 'brannebrona-300x132.jpg' (length=23)
  18 => string 'brannebrona-768x337.jpg' (length=23)
  19 => string 'brannebrona.jpg' (length=15)

($uniqe_times) (key intact with $files)
  0 => int 1536697041
  8 => int 1536696975
  9 => int 1536696959
  10 => int 1536696759
  13 => int 1536696758
  19 => int 1536696757

  (0-7 has the same filesize)
  (10-12 has the same filesize) 
  (13-18 has the same filesize)

  Go through the unique filesize array 
  and include the actual file you want
 */

 $use_files = array();
foreach($uniqe_times as $key=>$t) {
    $use_files[] = $files[$key];
}

//Go head and use the array $use_files
//Files you don't need (when it's files
//that are not supposed to be used, then delete them)
foreach($files as $f) {
    //If this file doesn't exists in use_files array, then remove
    if (in_array($f, $use_files) === false) {
        $unlink($f);
    }
}