在php中多个循环来激活目录和数据库

I have this php code to list all files in a directory and all the file also saved in database with file description, so am having problem to combine the data from directory listing and database content.

My question is how do i join the tow so if the file in directory has description saved in database it will display it if not it will be null and other file will still display

Here is what i have done so far

<?php

                $pathTo = __DIR__ . '/';
                $thelist = '';
                $hidefiles = array('.htaccess','index.php');
                $knownextention = array('bat');

                if( is_dir($pathTo) && file_exists($pathTo) ){

          if ($handle = opendir($pathTo)) {
            while (false !== ($file = readdir($handle))) {

              if ($file != "." && $file != ".." && !in_array($file, $hidefiles)) {

                  if(!is_dir($pathTo.'/'.$file)){
                        $extn = pathinfo($file, PATHINFO_EXTENSION);
                    }else if(is_dir($pathTo.'/'.$file)){
                        $extn = "Directory";
                    }else{
                        $extn = "Unknown";
                    }


                if(!in_array($extn, $knownextention) && $extn != "Directory"){
                    $filename = 'unknown';
                    $keybtn = 'javascript:void(0)'; 

                }else{
                    $filename = $file;
                    $keybtn = '?root='.$file;
                }
                // The post time is saved in Database and info
                // In my database i also save the file name and file type (Image | File | Folder)

                $db = new DBController();
                $db->prepare('SELECT * FROM userfile ORDER BY exetype DESC');
                $db->execute();
                $uFile = $db->getAll();
                $db->free();
                    if(!is_null($uFile)){
                        foreach($uFile as $row){
                            $id = $row->id;
                            $type = $row->type; //(Image | File | Folder)
                            $userfilename = $row->userfilename; //Name of the file saved in directory
                            $postTime = $row->postTime; //Time posted
                            $Info = $row->Info; //Discription about the file


                            //if($userfilename == $filename && !empty($Info)){
                                //echo $userfilename. ' | '.$type.' | '.$Info.'<br/>';
                            //}
                        }
                    }

                $thelist .= '<tr style="border-bottom: 1px solid #eee;"><td><a href="'.$keybtn.'">'.$filename.'</a></td>';
                $thelist .= '<td>'.$extn.'</td>';
                $thelist .= '<td>'.$postTime.'</td>';
                $thelist .= '<td>'.$Info.'</td></tr>';
              }
            }
            closedir($handle);
            sort($handle)
          }
         }//Dir Exist 
      else{
          //Directory not found      
      }
?>

In addition, how do i or it to list directories first before file?

There are at least two solutions that I can think of.

1. You will need to build two arrays keyed off of at least filename. ie, loop through all the files, and build an array like this

$db = new DBController();
$db->prepare('SELECT * FROM userfile ORDER BY exetype DESC');
$db->execute();
$uFile = $db->getAll();
$db->free();
foreach($uFile as $f) {
    $files_array[$f['filename']] = $f['filename'];
    $files_array[$f['filename']] = $f['description'];   
}

This code needs to be run outside your while loop. This will build a list of all the files in the database (I don't know what your field names are, you will need to fix that part). Inside your while loop, you can then do an isset() on $files_array to see if the file exists in the database.

Do the same thing with the results of your database query, then you can loop through one of the two arrays, and compare the keys. If the key exists, put in the data you want, if it doesn't, put in NULL.

2. Everytime you find a file, you can make a database query with that filename. If the query returns a result, add the data you want, else put in NULL values.

$db->prepare('SELECT * FROM userfile WHERE filename = ? ORDER BY exetype DESC');