PHP gets all the files in the directory and sorts them by time

Source: Internet
Author: User

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.