How php files are sorted by modification time
This article mainly introduces how to sort php files by modification time, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Php files are sorted by modification time: 1, through the "function printdir ($dir) {...}" method to traverse the files under the directory; 2, through the "function arraysort ($aa) {...}" method to sort the returned array by modification time.
This article operating environment: Windows7 system, PHP7.4 version, Dell G3 computer.
Example of php traversing files in a directory and sorting by modification time
Php traverses the files under the directory:
/ / traverse the file method function printdir ($dir) {$files = array () under the directory / / opendir () Open the directory handle if ($handle = @ opendir ($dir)) {/ / readdir () from the directory handle (resource Previously opened by opendir () to read entries, / / if not, return false while (($file = readdir ($handle))! = = false) {/ / read entry if ($file! = ".." & $file! = ".") {/ / exclude the root directory if (is_dir ($dir. "/". $file) {/ / if file is a directory, recursive $files [$file] = printdir ($dir. "/". $file);} else {/ / get the file modification date $filetime = date ('Y-m-d Hlav ifiletime slots, filemtime ($dir.) "/". $file); / / File modification time as a healthy value $files [$filetime] = $file;} @ closedir ($handle); return $files;}}
Sort the returned array by time
/ / sort the array by modification time function arraysort ($aa) {if (is_array ($aa)) {ksort ($aa); foreach ($aa as $key = > $value) {if (is_array ($value)) {$arr [$key] = arraysort ($value);} else {$arr [$key] = $value }} return $arr;} else {return $aa;}} $dir = "/ php"; / / output all files under / php print_r (arraysort (printdir ($dir) Thank you for reading this article carefully. I hope the article "how to sort php files by modification time" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!