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?