【问题标题】:PHP script to delete thousands of .jpg images?PHP脚本删除数千个.jpg图像?
【发布时间】:2011-10-18 17:45:09
【问题描述】:

我的弱共享网络主机不支持 cron 或 perl,我经常需要从某些文件夹中删除数千张 .jpg 图像。图像是从网络摄像头上传的。我想知道是否有一个简单的应用程序可以递归地找到所有 .jpg 图像并删除它们。

我需要能够仅定位以下日期格式的图片:2011-10-19_00-29-06.jpg ... 并且只能定位超过 48 小时的图片。

Apache 2.2.20 DirectAdmin 1.39.2 MySQL 5.1.57 php 5.2.17

【问题讨论】:

  • 如果你有 DirectAdmin,你可能有 shell,所以你可以用一个命令删除所有 jpgs find /dirname_where_jpgs_located -iname '\*.jp?eg' -exec rm -rf {} \;
  • 感谢您的快速回复...主机不允许 ssh :(您知道一个带有 gui 的 php 应用程序可以让非程序员轻松完成此过程吗?感谢您的任何提示。
  • 您将获得大量建议,因此您不需要任何 gui :))

标签: php loops delete-file file-attributes lastaccesstime


【解决方案1】:

@user427687,你的意思是所有图片格式2011***.jpg吗?如果是这样,我的代码可能会起作用。

<?php
  $path = dirname(__FILE__).'/filepath';
  if ($handle = opendir($path)) {
    while (false !== ($file = readdir($handle))) {
        if ((time()-filectime($path.'/'.$file)) < 86400*2) {
          if (preg_match('/\2011(.*?).jpg$/i', $file)) {
            unlink($path.'/'.$file);
          }
          if (preg_match('/\2011(.*?).jpeg$/i', $file)) {
            unlink($path.'/'.$file);
          }
        }
    }
  }
?>

【讨论】:

    【解决方案2】:

    一个简单的幼稚版本:

    $yesterday = date('Y-m-d', strtotime('yesterday')); // 2011-10-17
    $day_before = date('Y-m-d', strtotime('2 days ago')); // 2011-10-16
    
    $images = glob('*.jpg');
    
    foreach($images as $img) {
        if (strpos($img, $yesterday) === 0) || (strpos($img, $day_before) === 0)) {
            continue;
        }
        unlink($img);
    }
    

    这将删除所有带有 3 天或更早日期戳的文件,方法是检查文件是昨天还是昨天前一天。但它也会删除今天创建的所有文件。

    更好的版本是:

    $images = glob("*.jpg");
    foreach ($images as $img) {
         $ctime = filectime($img);
         if ($ctime < (time() - 86400 * 2)) {
             unlink($img);
         }
    }
    

    此版本检查文件的实际最后修改时间,并删除超过 48 小时的任何内容。但是,它会更慢,因为 filectime() 执行的 stat() 调用将是一个不便宜的调用。

    【讨论】:

    • Marc,感谢您的快速响应......如果我只想定位文件名中包含文本“2011”的 .jpg 图像?示例:2011-10-18_16-07-48.jpg 再次感谢
    【解决方案3】:

    这样的事情应该让你开始:

    class MyRecursiveFilterIterator extends RecursiveFilterIterator {
        const EXT = '.jpg';
        public function accept() {
            // code that checks the extension and the modified date
            return $this->current()->getFilename() ...
        }
    }
    
    $dirItr    = new RecursiveDirectoryIterator('/sample/path');
    $filterItr = new MyRecursiveFilterIterator($dirItr);
    $itr       = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST);
    
    // to iterate the list
    foreach ($itr as $filePath => $fileInfo) {
        echo $fileInfo->getFilename() . PHP_EOL;
    }
    

    【讨论】:

      【解决方案4】:

      或者只用php:

      <?php
      
      $last_2_days_in_seconds = 3600 * 48;
      
      foreach (glob("*.jpg") as $filename) {
        if((time() - fileatime($filename)) > $last_2_days_in_seconds && preg_match('/^2011/', $filename)) unlink($filename);
      }
      ?>
      

      【讨论】:

      • atime 不是最好的。理论上,所有这些图像都可以在过去 10 分钟内被查看,在这种情况下,NONE 将被删除。
      • 你想说文件访问时间不会改变?怎么可能?能给我举个例子吗?我很感兴趣。
      • atime 是上次有人访问文件或更改其元数据的时间。只需在图像查看器中打开/查看 jpg 将更新其时间,因为它已被“访问”。 ctime 和 mtime 更可靠,因为它们只有在有人更改文件时才会更改(ctime,用于 inode-data)或文件的内容更改(mtime)。
      • 感谢您的快速回复......如果我只想定位文件名中包含文本“2011”的 .jpg 图像?示例:2011-10-18_16-07-48.jpg 再次感谢
      • 但是将 fileatime 更改为 filemtime 或使用 (stat)[php.net/stat] 检查您想要的所有内容都不是问题,这正是我认为作者的方式想做他的任务
      猜你喜欢
      • 2015-09-23
      • 2015-08-13
      • 1970-01-01
      • 2014-08-27
      • 2019-07-28
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      相关资源
      最近更新 更多