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>在线网盘</h2>
<!--目录列表-->
<div>
    您的位置:主目录
</div>
<!--文件列表-->
<table border="1" cellpadding="3" cellspacing="0" width="100%">
    <tr bgcolor="skyblue"><th>文件名</th><th>大小</th><th width="30%">上传时间</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="">复制</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="">复制</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="">复制</a>
                |<a href="">删除</a>
            </td>
        </tr>
</table>
<form method="post">
    新建文件夹:<input type="text" name="newfolder">
    <input type="submit" value="创建">
</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
 */
 //初始化数据库连接
function dbInit(){
    $link=mysql_connect('localhost','root','root');
    if(!$link){
        die('lian连接数据库失败'.mysql_error());
    }
    //设置字符集,选择数据库
    mysql_query('set names utf8');
    mysql_query('use php');
}
//查询数据库显示错误信息
function query($sql){
    if($result=mysql_query($sql)){
        //执行成功
        return $result;
    }else{
        //执行失败,显示错误信息以便于调试程序
        echo 'sql执行失败:<br>';
        echo '错误的sql为:',$sql,'<br>';
        echo '错误的代码为:',mysql_errno(),'<br>';
        echo '错误的信息为:',mysql_error(),'<br>';
        die();
    }
}
//查询所有数据并返回结果集
function fetchAll($sql){
    //执行query()函数
    if($result=query($sql)){
        //执行成功
        //遍历结果集
        $rows=array();
        while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
            $rows[]=$row;
        }
        //释放结果集资源
        mysql_free_result($result);
        return $rows;
    }else{
        //执行失败
        return false;
    }
}
//查询单条数据并返回结果集
function fetchRow($sql){
    //执行query()函数
    if($result=query($sql)){
        //从结果集取得依次数据即可
        $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");
//载入数据库操作文件
require("./public_sql.php");
//初始化连接数据库操作
dbInit();
//获取当前目录的id
$folder_id=isset($_GET['folder'])?intval($_GET['folder']):0;
//网盘文件列表
//请求目录不是根目录时,获取当前访问目录的信息
$path=array();
if($folder_id!=0){
    //根据当前目录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'];
    //根据ID路径查询所有父级目录的信息
    if($file_ids!=""){
        $sql="select folder_id,folder_name from netdisk_folder where folder_id in($file_ids)";
        $path=fetchAll($sql);
        //将当期目录追加到路劲数组的末尾
        $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>";
//引进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>上传时间</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="">复制</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="">复制</a>|
                <a href="">删除</a></td>
        </tr>
    <?php endforeach;?>
</table>

4, the page display

is as follows:

微信图片_20180302171629.png


5, open the directory function

Just add the following code to the open button

微信图片_20180303083813.png

The code is as follows:

<?php
<a href="?folder=<?php echo $v['folder_id']?>">打开</a>


Continuing Learning
||
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/2 0002 * Time: 上午 10:07 */ ?> <h2>在线网盘</h2> <!--目录列表--> <div> 您的位置:主目录 </div> <!--文件列表--> <table border="1" cellpadding="3" cellspacing="0" width="100%"> <tr bgcolor="skyblue"><th>文件名</th><th>大小</th><th width="30%">上传时间</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="">复制</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="">复制</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="">复制</a> |<a href="">删除</a> </td> </tr> </table> <form method="post"> 新建文件夹:<input type="text" name="newfolder"> <input type="submit" value="创建"> </form> <form method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form>
submitReset Code