文件目录相关函数 <?php // output files in the folder function outputcurfiles ($allowedtypes, $thedir){ //First, we ensure that the directory exists. if (is_dir ($thedir)){ //Now, we scan the files in this directory using scandir. $scanarray = scandir ($thedir); //Then we begin parsing the array. //Since scandir() counts the "." and ".." file navigation listings //as files, we should not list them. for ($i = 0; $i < count ($scanarray); $i++){ if ($scanarray[$i] != "." && $scanarray[$i] != ".."){ //Now, we check to make sure this is a file, and not a directory. if (is_file ($thedir . "/" . $scanarray[$i])){ //Now, since we are going to allow the client to edit this file, //we must check if it is read and writable. if (is_writable ($thedir. "/" . $scanarray[$i]) && is_readable($thedir . "/" . $scanarray[$i])){ //Now, we check to see if the file type exists within our allowed type array. $thepath = pathinfo ($thedir . "/" . $scanarray[$i]); if (in_array ($thepath['extension'], $allowedtypes)){ //If the file follows our stipulations, then we can proceed to output it. echo $scanarray[$i] . "<br />"; } } } } } } else { echo "Sorry, this directory does not exist."; } } $allowedtypes = array ("txt","html"); outputcurfiles ($allowedtypes, "testfolder"); /// // function recurdir ($thedir) { //First attempt to open the directory. try { if ($adir = opendir ($thedir)){ //Scan through the directory. while (false !== ($anitem = readdir ($adir))){ //Do not count the . or .. in a directory. if ($anitem != "." && $anitem != ".."){ //Now, if it is another directory, then we indent a bit //and go recursive. if (is_dir ($thedir . "/" . $anitem)){ ?><span style="font-weight: bold;" mce_style="font-weight: bold;"><?php echo $anitem; ?></span><?php ?><div style="margin-left: 10px;" mce_style="margin-left: 10px;"><?php recurdir ($thedir . "/" . $anitem ); ?></div><?php } elseif (is_file ($thedir . "/" . $anitem)){ //Then echo the file. echo $anitem . "<br />"; } } } } else { throw new exception ("Sorry, directory could not be openend."); } } catch (exception $e) { echo $e->getmessage(); } } echo "<br />/<br /><br />"; recurdir("testfolder"); // echo "<br />/<br /><br />"; function sortfilesbydate ($thedir){ //First, we ensure that the directory exists. if (is_dir ($thedir)){ //Now, we scan the files in this directory using scandir. $scanarray = scandir ($thedir); $finalarray = array(); //Then we begin parsing the array. //Since scandir() counts the "." and ".." file navigation listings //as files, we should not list them. for ($i = 0; $i < count ($scanarray); $i++){ if ($scanarray[$i] != "." && $scanarray[$i] != ".."){ //Now, we check to make sure this is a file, and not a directory. if (is_file ($thedir . "/" . $scanarray[$i])){ //Now what we need to do is cycle the data into an associative array. $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]); } } } //Finally, when we have gone through the entire array, we simply asort() it. asort ($finalarray); return ($finalarray); } else { echo "Sorry, this directory does not exist."; } } //We then call the function pointed to the directory we want to look through. $sortedarray = sortfilesbydate ("testfolder"); //We could then output the as such: while ($element = each ($sortedarray)){ echo "File: " . $element['key'] . " was last modified: " . date ("F j, Y h:i:s", $element['value']) . "<br />"; } ?>