0

ディレクトリ内の最も古いファイル、すべての中で最も古いファイル (特定のフォルダのディレクトリ、サブディレクトリを含む) を見つける必要があります。

   Folder 1
       - dir 1
          -  dir 1.1
             - file 1
       - dir 2
          - dir 2.1
             - file 2

ファイル 2 が最も古い場合、メイン ディレクトリ名 (フォルダ 1) を渡して最も古いファイル (ファイル 2) を取得できるようにする必要があります。

私の解決策は

    function get_oldest_file($dir) {
    $filemdate = array();
    print $dir.PHP_EOL;
    if ($handle = opendir($dir)) {
     while (false !== ($file = readdir($handle))) {
          $files[] = $file;
      print $file.PHP_EOL;
    }
    foreach ($files as $eachfile) {
        if (is_file($dir.eachfile)) {
          $file_date[$eachfile] = filemtime($dir.$eachfile);
          print $filemdate[$eachfile].PHP_EOL;
        }
    }
}
closedir($handle);
asort($filemdate, SORT_NUMERIC);
reset($filmdate);
$oldest = key($filemdate);
print "Oldest is : ".$oldest;
return $oldest;
}

 echo get_oldest_file("/path/---")

ありがとう !

4

1 に答える 1

1

どうですか?

function workerFunction($currentDir, $oldestFile)
    foreach (glob($currentDir.'/*') as $file) {
        if (is_dir($file)) {
            if ((basename($file)!='.') && (basename($file)!='..')) {
                 $oldestFile = workerFunction($file, $oldestFile);
            }
        } else {
            $mtime = filemtime($file);
            if ($mtime <= $oldestFile['mtime']) {
                $oldestFile['mtime'] = $mtime;
                $oldestFile['path'] = $file;
            }
        }
    }
    return $oldestFile;
}


function searchForOldestFile($dir) {
    $oldestFile['mtime'] = time();
    $oldestFile['path'] = null;
    $oldestFile = workerFunction($dir, $oldestFile);
    return $olderstFile['path'];
}

デバッグ用の PHP 環境はありませんが、少なくともいくつかの小さな修正があれば、必要に応じて動作する可能性があります。簡単に使用できるように説明します。 searchForOldestFile 関数は、スクリプトが呼び出す必要があるインターフェイスです。workerFunction は「魔法」を実行し (機能する場合:))、現在の最も古いファイルへの参照を $oldestFile に保持します。

于 2013-10-01T18:45:21.130 に答える