Network disk file directory function
1, edit the html page
Create a new netdisk_html.php file:
Design the network disk front-end page, the code is as follows:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/2 0002 * Time: 上午 10:07 */ ?> <h2>在線網(wǎng)盤</h2> <!--目錄列表--> <div> 您的位置:主目錄 </div> <!--文件列表--> <table border="1" cellpadding="3" cellspacing="0" width="100%"> <tr bgcolor="skyblue"><th>文件名</th><th>大小</th><th width="30%">上傳時(shí)間</th><th>操作</th></tr> <!--目錄列表--> <tr> <td>目錄1</td> <td>-</td> <td>2018-03-02 15:57:56</td> <td align="center"> <a href="">打開</a> |<a href="">復(fù)制</a> |<a href="">刪除</a> </td> </tr> <tr> <td>目錄2</td> <td>-</td> <td>2018-03-02 15:58:00</td> <td align="center"> <a href="">打開</a> |<a href="">復(fù)制</a> |<a href="">刪除</a> </td> </tr> <!--文件列表--> <tr> <td>1.jpg</td> <td>3KB</td> <td>2018-03-02 16:06:12</td> <td align="center"> <a href="">下載</a> |<a href="">復(fù)制</a> |<a href="">刪除</a> </td> </tr> </table> <form method="post"> 新建文件夾:<input type="text" name="newfolder"> <input type="submit" value="創(chuàng)建"> </form> <form method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上傳"> </form>
2, establish database connection
The basic display page is displayed, the data is written casually, the next step should be to establish the connection with the database Connect, traverse the information in the database and display it on the front-end page. Since many operational queries and connections are used later, we write a tool class to facilitate subsequent database operations. Create a new public_sql.php file:
The code is as follows:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/2 0002 * Time: 上午 9:42 */ //初始化數(shù)據(jù)庫連接 function dbInit(){ $link=mysql_connect('localhost','root','root'); if(!$link){ die('lian連接數(shù)據(jù)庫失敗'.mysql_error()); } //設(shè)置字符集,選擇數(shù)據(jù)庫 mysql_query('set names utf8'); mysql_query('use php'); } //查詢數(shù)據(jù)庫顯示錯(cuò)誤信息 function query($sql){ if($result=mysql_query($sql)){ //執(zhí)行成功 return $result; }else{ //執(zhí)行失敗,顯示錯(cuò)誤信息以便于調(diào)試程序 echo 'sql執(zhí)行失敗:<br>'; echo '錯(cuò)誤的sql為:',$sql,'<br>'; echo '錯(cuò)誤的代碼為:',mysql_errno(),'<br>'; echo '錯(cuò)誤的信息為:',mysql_error(),'<br>'; die(); } } //查詢所有數(shù)據(jù)并返回結(jié)果集 function fetchAll($sql){ //執(zhí)行query()函數(shù) if($result=query($sql)){ //執(zhí)行成功 //遍歷結(jié)果集 $rows=array(); while($row=mysql_fetch_array($result,MYSQL_ASSOC)){ $rows[]=$row; } //釋放結(jié)果集資源 mysql_free_result($result); return $rows; }else{ //執(zhí)行失敗 return false; } } //查詢單條數(shù)據(jù)并返回結(jié)果集 function fetchRow($sql){ //執(zhí)行query()函數(shù) if($result=query($sql)){ //從結(jié)果集取得依次數(shù)據(jù)即可 $row=mysql_fetch_array($result,MYSQL_ASSOC); return $row; }else{ return false; } }
3, traverse the database data and display it to the front-end page
After the database connection public class is written, the database is queried to traverse and display the directory files. At the same time, the corresponding different directories can be traversed according to $folder_id
:
New index.php file:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/2 0002 * Time: 上午 9:39 */ header("Content-Type:text/html;charset=utf-8"); //載入數(shù)據(jù)庫操作文件 require("./public_sql.php"); //初始化連接數(shù)據(jù)庫操作 dbInit(); //獲取當(dāng)前目錄的id $folder_id=isset($_GET['folder'])?intval($_GET['folder']):0; //網(wǎng)盤文件列表 //請求目錄不是根目錄時(shí),獲取當(dāng)前訪問目錄的信息 $path=array(); if($folder_id!=0){ //根據(jù)當(dāng)前目錄ID查詢目錄列表 $sql="select folder_name,folder_path from netdisk_folder where folder_id=$folder_id"; $current_folder=fetchRow($sql); $file_ids=$current_folder['folder_path']; //根據(jù)ID路徑查詢所有父級目錄的信息 if($file_ids!=""){ $sql="select folder_id,folder_name from netdisk_folder where folder_id in($file_ids)"; $path=fetchAll($sql); //將當(dāng)期目錄追加到路勁數(shù)組的末尾 $path[]=array( 'folder_id'=>$folder_id, 'folder_name'=>$current_folder['folder_name'] ); } } //獲取指定目錄下的所有文件夾 $sql="select folder_id,folder_name,folder_time from netdisk_folder where folder_pid=$folder_id"; $folder=fetchAll($sql); //獲取指定目錄下的所有文件 $sql="select file_id,file_name,file_save,file_size,file_time from netdisk_file where folder_id=$folder_id "; $file=fetchAll($sql); //echo "<pre>"; //print_r($folder); //echo "</pre>"; //引進(jìn)html頁面 require('netdisk_html.php');
The above code gets a $folder and a $file, which represent directories and files respectively and are both a two-dimensional array. After saving the information in the database
, the front-end file is introduced through require('netdisk_html.php'). Next, you only need to traverse $folder and $file and display them in the table. You can
Add the code to traverse the data in the file list and directory list content of netdisk_html.php:
<?php <!--文件列表--> <table border="1" cellpadding="3" cellspacing="0" width="60%"> <tr bgcolor="skyblue"><th>文件名</th><th>大小</th><th>上傳時(shí)間</th><th>操作</th></tr> <!--目錄列表--> <?php foreach ($folder as $v): ?> <tr> <td><?php echo $v['folder_name']?></td> <td>-</td> <td><?php echo $v['folder_time']?></td> <td align="center"> <a href="">打開</a> |<a href="">復(fù)制</a> |<a href="">刪除</a> </td> </tr> <?php endforeach;?> <!--文件列表--> <?php foreach ($file as $v):?> <tr> <td> <?php echo $v['file_name'] ?> </td> <td><?php echo round($v['file_size']/1024) ?>KB</td> <td><?php echo $v['file_time'] ?></td> <td align="center"><a href="">下載</a>| <a href="">復(fù)制</a>| <a href="">刪除</a></td> </tr> <?php endforeach;?> </table>
4, the page display
is as follows:
5, open the directory function
Just add the following code to the open button
The code is as follows:
<?php <a href="?folder=<?php echo $v['folder_id']?>">打開</a>