douzhang6496 2016-11-03 17:29
浏览 26

在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?

  • 写回答

1条回答 默认 最新

  • dongyuling0312 2016-11-03 20:17
    关注

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改