List directory contents by date
Loads the contens of a dir an sorts them by the date of creation.
function listdir_by_date($path){ $dir = opendir($path); $list = array(); while($file = readdir($dir)){ if ($file != '.' and $file != '..'){ // add the filename, to be sure not to // overwrite a array key $ctime = filectime($data_path . $file) . ',' . $file; $list[$ctime] = $file; } } closedir($dir); krsort($list); return $list; }
Snippet Details
-
AuthorJonas John
-
LicensePublic Domain
-
LanguagePHP
-
Created03/29/2006
-
Updated03/29/2006
-
Tagsdirectory functions, folder, files
listdir_by_date('data/');
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
rob_v February 05, 2010 at 18:32
I always found this alot easier:
$file = '*';
$dir = '/path/to/wherever/';
$sorted_array = listdir_by_date($dir.$file);
function listdir_by_date($pathtosearch)
{
foreach (glob($pathtosearch) as $filename)
{
$file_array[filectime($filename)]=basename($filename); // or just $filename
}
ksort($file_array);
return $file_array;
}
$file = '*';
$dir = '/path/to/wherever/';
$sorted_array = listdir_by_date($dir.$file);
function listdir_by_date($pathtosearch)
{
foreach (glob($pathtosearch) as $filename)
{
$file_array[filectime($filename)]=basename($filename); // or just $filename
}
ksort($file_array);
return $file_array;
}
Rick December 14, 2009 at 21:38
Groovy! Real time saver. Thanks!
Rolf October 26, 2009 at 22:43
$data_path is apparently supposed to be "$path".
Alexander Doche February 18, 2009 at 17:50
should of cleaned that up a bit first, sorry :
Alexander Doche February 18, 2009 at 17:45
just replace $data_path with $path
works fine for me
you can print the list by making a for loop or just by print_r($list)
so i basically did this as a test to see if it works
$path = '../upload/pubvoyages/';//$_SERVER['DOCUMENT_ROOT'] . '/fr/pubvoyages/';
$list = listdir_by_date($path);
print_r ($list);
function listdir_by_date($path){
$dir = opendir($path);
$list = array();
while($file = readdir($dir)){
if ($file != '.' and $file != '..'){
// add the filename, to be sure not to
// overwrite a array key
$ctime = filectime($path . $file) . ',' . $file;
$list[$ctime] = $file;
}
}
closedir($dir);
krsort($list);
return $list;
}
works fine for me
you can print the list by making a for loop or just by print_r($list)
so i basically did this as a test to see if it works
$path = '../upload/pubvoyages/';//$_SERVER['DOCUMENT_ROOT'] . '/fr/pubvoyages/';
$list = listdir_by_date($path);
print_r ($list);
function listdir_by_date($path){
$dir = opendir($path);
$list = array();
while($file = readdir($dir)){
if ($file != '.' and $file != '..'){
// add the filename, to be sure not to
// overwrite a array key
$ctime = filectime($path . $file) . ',' . $file;
$list[$ctime] = $file;
}
}
closedir($dir);
krsort($list);
return $list;
}
guest August 04, 2008 at 08:31
what is: $data_path in:
$ctime = filectime($data_path . $file) . ',' . $file;
???
$ctime = filectime($data_path . $file) . ',' . $file;
???