I want to get all the file name from a directory, and sort by last modified date. I googled and get lots of answers, so I picked one from sort files by date in PHP
<?php
$files = array();
$dir = getcwd();
$dir = dirname($dir); //get Parent directory
$dir .= "/XMLdata/*.xml";
$files = glob($dir);
usort($files, function($a, $b) {
return filemtime($a) < filemtime($b);
});
header('Content-type: application/json');
echo json_encode($files);
?>
From the code above, I can get the all the xml file from the directory XMLdata, However, from the result $files. I get all the files, but with absolute Path, which is "c:\wamp\www/XMLdata/xxxx.xml", and I just want the file xxxx.xml. I could process it in javascript use regular expression, But I think there must be a better way to get just the file name. Can anyone help me, thanks.
If there is no good way, I can use
str = result[numberIndex];//one of absolute path from php
var array = str.split("/");
str = array[array.length - 1];
I can get the right filename.
Idea from @born loser
<?php
$files = array();
$dir = getcwd();
$dir = dirname($dir); //get Parent directory
$dir .= "/XMLdata/*.xml";
$files = glob($dir);
usort($files, function($a, $b) {
return filemtime($b) < filemtime($a);
});
foreach($files as &$value)
{
$value = substr($value,strrpos($value,'/')+1);
}
header('Content-type: application/json');
echo json_encode($files);
?>