PHP gets all the file names in the directory
1. First open the directory to operate and point to it with a variable
Open the subdirectory under PIC under the current directory common.
$handler = Opendir (' Pic/common ');
2. Read all files in the directory under the loop
/* Where $filename = Readdir ($handler) assigns the read file name to the $filename each time the loop is cycled, so that $filename!== false in order not to be trapped in a dead loop. Be sure to use!==, because if a filename is called ' 0′, or if some of the systems think it is false, the!= will stop the loop.
while (($filename = Readdir ($handler))!== false)
{
3, the directory will have two files, the name is '. ' and '.. ', do not operate on them
if ($filename!= "." && $filename!= "...")
{
4, to deal with
Here it simply uses echo to output the file name
echo $filename;
}
}
5, close the directory
Closedir ($handler);
php gets all the files in the directory and sorts them by file creation time, PHP is sorted by file creation/modification time
function GetFile ($dir = ' ") {
$num = 1; Used to record the number of files in the directory
$dirname = '. /testdir '; Name of directory to traverse
$dir _handle=opendir ($dirname);
while ($file =readdir ($dir _handle)) {
if ($file!= "." && $file!= "..." && $file!= "index.php") {
$key = Filectime ($file) *1000+rand (100,999); Generate key based on file creation time
$files [$key] = $file;
$num + +;
}
}
Ksort ($files);
foreach ($files as $ky => $val) {
$dirFile = $dirname. " /". $val;
$res. = Tabledata ($num. ' |<a href= '. $val. ' "target=" _blank ">". $val. ' </a>| '. FileSize ($dirFile). ' | FileType ($dirFile). ' | Date ("y/n/t h:i:s", Filemtime ($dirFile));
}
return $res;
Closedir ($dir _handle);
}