0

関数でディレクターを検索しglob、一致したファイルのリストを取得します。filemtime次に、ファイルをチェックしてマップを作成します。次に、ファイルの日付に関してマップを並べ替えます。最後に、最新のファイル名と変更時刻を取得します。私のコードはこのようなものです。小さなディレクトリではうまく機能しますが、大きなディレクトリでは遅くなります。より速い/賢い方法があるのだろうか?

$fileList = array();
// $id = is the argument of this function
$files = glob('myfolder'.DS.'someone'.$id.'*.txt');
if (empty($files)) {
    return 0;
}
foreach ($files as $file) {
    $fileList[filemtime($file)] = $file;
}

if (sizeof($files) > 1) {
    ksort($fileList);
}

$latestFilename = end($fileList);
$fileLastModifDate = filemtime( $latestFilename );
4

1 に答える 1

0

i suspect that your "mapping" is actually creating a hash table, and then the sort is not efficiant as one might expect, i would try this: (this is the basic, u can fancy it up)

class file_data
{
   public  $time; // or whatever u use
   public $file;
}

$arr =new array ();
foreach ($files as $file) {
     $new_file = new file_data ();
     $file_data->time = filetime($file);
     $file_data->file = $file;   
     array_push ($arr,$file_data);
}

function file_object_compare ($a,$b)
{
    if ($a->time > $b->time) return -1;
//    etc... i.e 0 eual 1 otherwise

}

//and then u sort
usort ($arr,"file_object_compare");

// or // and this is probably better, if u only need this paricular sorting

function file_object_compare ($a,$b)
{
    if (filetime($a)> filetime($b)) return -1;
//    etc... i.e 0 eual 1 otherwise    
}

usort ($files,"file_object_compare");
于 2013-05-18T21:12:29.713 に答える