<?php $dir = "/x/"; // directory chdir($dir); array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files); foreach($files as $filename) { /*echo "<li>".substr($filename, 0, -4)."</li>";*/ //ตัดนามสกุล echo "<li>".substr($filename, 0)."</li>"; } ?>
<?php //This little PHP script is the most elegant way //I could find the list directories and //files with PHP and sort by date //thanks to StackOverflow $files = array(); $dir = new DirectoryIterator('.'); foreach ($dir as $fileinfo) { $files[$fileinfo->getMTime()] = $fileinfo->getFilename(); } //krsort will sort in reverse order krsort($files); //just print out the file names //excluding this file (named index.php and the dir "." ) foreach($files as $file){ if ($file == "index.php" or $file == "." ){ }else{ print $file; print "</br>"; } } ?>
<?php // list from a given folder $folder="test/"; $files = glob( $folder."*.*" ); // to avoid hidden files // Sort files by modified time, latest to oldest array_multisort(array_map( 'filemtime', $files ),SORT_NUMERIC,SORT_DESC,$files); // Use SORT_ASC in place of SORT_DESC for oldest to latest //array_multisort(array_map( 'filemtime', $files ),SORT_NUMERIC,SORT_ASC,$files); // display the file names if(count($files)){ for( $i=0 ; $i < count($files) ; $i++ ){ echo(basename($files[$i])." <a href='".$folder.$files[$i]."'>Link to the file</a><br>"); } } ?>
อันนี้ครบ แต่ไม่ขึ้นบรรทัด
<?php $path = __DIR__; if (!is_dir($path)) { throw new RuntimeException(sprintf('Path %s does not exist!', $path)); } if (!is_readable($path)) { throw new RuntimeException(sprintf('Path %s is not readable!', $path)); } $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($path) ); $files = []; foreach ($iterator as $fileInfo) { if ($fileInfo->isFile()) { $files[] = [ $fileInfo->getFilename(), dirname($fileInfo->getPathname()), date('c', $fileInfo->getMTime()) ]; } } print_r($files); ใส่แบบนี้จะแบ่งเป็นชุด echo '<pre>'; print_r($files); echo '</pre>';
ตัวอย่างอื่น
อันนี้แบ่งเป็น ชุด <pre> <?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z')); print_r ($a); ?> </pre> ผลออกมาแบบนี้ Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) อันนี้ แสดงยาว ติดกัน <?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z')); print_r ($a); ?> ผลออกมาแบบนี้ Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) )
ทำให้ขึ้นบรรทัดใหม่
<?php $path = __DIR__; if (!is_dir($path)) { throw new RuntimeException(sprintf('Path %s does not exist!', $path)); } if (!is_readable($path)) { throw new RuntimeException(sprintf('Path %s is not readable!', $path)); } $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($path) ); $files = []; foreach ($iterator as $fileInfo) { if ($fileInfo->isFile()) { echo $fileInfo->getFilename(); echo dirname($fileInfo->getPathname()); echo date('c', $fileInfo->getMTime()); echo "<br>"; } } ?>
print_r มันมีประโยชน์อะไรหว่า ในเมื่อมันทำอะไรต่อไม่ได้
ไว้ให้เรา debug โค้ด
ถ้าโปรแกรมใหญ่ขึ้น print_r เป็นสิ่งที่มีประโยชน์มาก
เพราะเป็นทางเดียว ที่จะดูโครงสร้างของ array ได้
นอกจาก var_dump ซึ่งมันก็เหมือนๆกัน
ถ้าเกิดต้องจัดการพวกโครงสร้างใหญ่ๆ จะเช็คได้ไงล่ะ ว่ากำลังจะเอาข้อมูลจากไหน
เช่น ตัวอย่างง่ายๆ ตอนอ่าน xml มาก้อนนึง มันก็เป็นโครงสร้างไรไม่รู้ เต็มไปหมด
อันนี้ได้
<?php $rootdir = $_SERVER['DOCUMENT_ROOT']; if ($dir = opendir($rootdir . '/x/')) { $filearray = array(); $i = 0; $exclusions = array('.', '..', 'inc', 'css', 'images'); while (false !== $file = readdir($dir)) { if (is_dir($rootdir . '/x/' . $file) && !in_array($file, $exclusions)) { $subdir = opendir($rootdir . '/x/' . $file); while (false !== $newfile = readdir($subdir)) { if (strpos($newfile,'.php',1) && !strpos($newfile,'.php.swp',1)) { $filearray[$i] = array(); $filearray[$i][] = $file.'/'.$newfile; $i++; } } } else { if (strpos($file,'.php',1) && !strpos($file,'.php.swp',1)) { $filearray[$i] = array(); $filearray[$i][] = $file; $i++; } } } // Append the modified dates for ($j = 0; $j < count($filearray); $j++) { $lastmodified = filemtime($rootdir . '/x/' . $filearray[$j][0]); $filearray[$j][] = date('m/j/y g:i a', $lastmodified); } // Sort the array by the modified dates function comparedates($page1, $page2) { if (strtotime($page1[1]) == strtotime($page2[1])) { return 0; } else if (strtotime($page1[1]) < strtotime($page2[1])) { return 1; } else { return -1; } } usort($filearray, 'comparedates'); // Trim array to the four most recently updated pages for ($m = count($filearray) - 1; $m > 3; $m--){ array_pop($filearray); } // Output the four sorted pages and dates for ($l = 0; $l < count($filearray); $l++) { echo '<li><a href="' . $rootdir . '/' . $filearray[$l][0].'">'.$filearray[$l][0].'</a> <span class="lastdate">('.$filearray[$l][1].')</span></li>'; } } ?>
<?php $files = array(); $dir = opendir('.'); // open the cwd..also do an err check. while(false != ($file = readdir($dir))) { if(($file != ".") and ($file != "..") and ($file != "index.php")) { $files[] = $file; // put in array. } } natsort($files); // sort. // print. foreach($files as $file) { echo("<a href='$file'>$file</a> <br />\n"); } ?>
<?php function getFiles(){ $files=array(); if($dir=opendir('.')){ while($file=readdir($dir)){ if($file!='.' && $file!='..' && $file!=basename(__FILE__)){ $files[]=$file; } } closedir($dir); } natsort($files); //sort return $files; } ?> <html> <head> </head> <body> <h1> List of files </h1> <ul class="dir"> <? foreach(getFiles() as $file) echo "<li name='$file'><a href='$file'>$file</a></li>"; ?> </ul> </body> </html>
<?php $dirname = "."; $dir = opendir($dirname); while(false != ($file = readdir($dir))) { if(($file != ".") and ($file != "..") and ($file != "index.php")) { $list[] = $file; } } sort($list); foreach($list as $item) { echo("<a href='$item'>$item</a> <br />"); } ?>
เทพเขียนไว้
keyword script php sort file by date github
Script for get the file list and allow sort it it by date of last modification, store files map and compare this map with previous stored map.
PHP script to get list of files and file comparison
PHP script to get the file list on the server and allow sort it by date of last modification, store the files map and compare this map with previously stored map. Useful to find changed/hacked files if you are not allowed use any version control system.
Features
- scan files using filter (sets in script options)
- store result in to the files.map
- comparison with previously saved a map
- sorting by path, filename, extension, size, last modification, last inode change, permissions, file state
Usage
Put script in to the server root folder or in to subfolder(then will be need chage $path option). Change the script options if need (e.g. current path, exclude filter, default map name). Run it! (open in the browser) As result you will get the full file list table and the stored file map. Script will make the new map file after each click on Scan again, that allow you compare the couple maps in future.
Warning!
Do not leave this script and the .map files with free access on the server! Use .htaccess for access control.
https://github.com/Fedik/filelist-php