文件管理系统 v1.0 PHP源码

2025-3-31 / 0 评论 / 10 阅读

PixPin_2025-03-31_11-40-58.png

演示站点 V1.2:http://linghu.rf.gd/FMS/index.php

使用说明

  1. 将代码保存为index.php文件

  2. 确保PHP环境已安装并运行

  3. 默认管理密码为8888(可在代码开头修改)

  4. 上传的文件会保存在uploads目录下

安全提示

这是一个简单的演示系统,不建议在生产环境中使用,因为它没有以下安全措施:

  • 用户认证

  • 文件上传类型检查

  • 防CSRF保护

  • 其他高级安全措施

如需在生产环境使用,请添加适当的安全措施。

新增功能说明

  1. 系统标题

    • 在页面顶部居中显示"种子文件管理系统"加粗标题

    • 使用大号字体和阴影效果美化

  2. 分页功能

    • 每页显示20个文件

    • 底部显示分页导航,包括页码和前后页按钮

    • 支持直接跳转到第一页和最后一页

  3. 批量管理功能

    • 文件列表添加复选框列

    • 提供"全选"和"取消全选"按钮

    • 支持批量删除选中的文件和文件夹

  4. 统计信息显示

    • 美观的统计卡片显示总文件数、文件夹数、文件数和当前页数

    • 使用卡片式设计,带有阴影效果

    • 数字突出显示,标签清晰

  5. 其他改进

    • 优化了页面布局和间距

    • 改进了分页导航的显示逻辑,最多显示5个页码

    • 增强了批量操作的交互体验

使用说明

  1. 批量操作:

    • 勾选文件前的复选框选择文件

    • 点击"全选"选择当前页所有文件

    • 点击"批量删除"删除选中的文件

  2. 分页导航:

    • 点击页码跳转到指定页

    • 使用"上一页"和"下一页"按钮导航

    • 可直接跳转到第一页或最后一页

  3. 统计信息:

    • 在文件列表上方显示当前目录的统计信息

    • 包括总文件数、文件夹数、文件数和当前页数

这个增强版系统既保持了单文件的简洁性,又增加了实用的批量管理和分页功能,同时通过美观的统计卡片提升了用户体验。

<?php
// 配置文件
$PASSWORD = '8888'; // 管理密码
$UPLOAD_DIR = 'uploads'; // 上传目录
$CURRENT_DIR = isset($_GET['dir']) ? rtrim($_GET['dir'], '/') : ''; // 当前目录
$ITEMS_PER_PAGE = 20; // 每页显示文件数

// 创建上传目录(如果不存在)
if (!file_exists($UPLOAD_DIR)) {
    mkdir($UPLOAD_DIR, 0755, true);
}

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 新建文件夹
    if (isset($_POST['create_folder'])) {
        $folder_name = trim($_POST['folder_name']);
        if (!empty($folder_name)) {
            $new_folder_path = ($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR . '/' . $folder_name;
            if (!file_exists($new_folder_path)) {
                mkdir($new_folder_path, 0755, true);
                $message = "文件夹创建成功!";
            } else {
                $error = "文件夹已存在!";
            }
        }
    }

    // 上传文件
    if (isset($_POST['upload'])) {
        $upload_password = $_POST['upload_password'];
        if ($upload_password === $PASSWORD) {
            $target_dir = ($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR . '/';

            // 处理多文件上传
            $uploaded_files = 0;
            foreach ($_FILES['files']['name'] as $key => $name) {
                if ($_FILES['files']['error'][$key] === UPLOAD_ERR_OK) {
                    $target_file = $target_dir . basename($name);
                    $temp_file = $_FILES['files']['tmp_name'][$key];

                    // 检查文件是否已存在
                    if (!file_exists($target_file)) {
                        if (move_uploaded_file($temp_file, $target_file)) {
                            $uploaded_files++;
                        }
                    }
                }
            }

            if ($uploaded_files > 0) {
                $message = "成功上传 {$uploaded_files} 个文件!";
            } else {
                $error = "没有文件被上传或文件已存在!";
            }
        } else {
            $error = "密码错误!";
        }
    }

    // 删除文件或文件夹
    if (isset($_POST['delete'])) {
        $delete_password = $_POST['delete_password'];
        if ($delete_password === $PASSWORD) {
            $target = $_POST['target'];
            $full_path = ($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR . '/' . $target;

            if (file_exists($full_path)) {
                if (is_dir($full_path)) {
                    // 删除文件夹(递归)
                    $it = new RecursiveDirectoryIterator($full_path, RecursiveDirectoryIterator::SKIP_DOTS);
                    $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
                    foreach($files as $file) {
                        if ($file->isDir()){
                            rmdir($file->getRealPath());
                        } else {
                            unlink($file->getRealPath());
                        }
                    }
                    rmdir($full_path);
                    $message = "文件夹删除成功!";
                } else {
                    // 删除文件
                    unlink($full_path);
                    $message = "文件删除成功!";
                }
            }
        } else {
            $error = "密码错误!";
        }
    }

    // 批量删除
    if (isset($_POST['batch_delete'])) {
        $delete_password = $_POST['batch_delete_password'];
        if ($delete_password === $PASSWORD && isset($_POST['selected_files'])) {
            $deleted_count = 0;
            foreach ($_POST['selected_files'] as $target) {
                $full_path = ($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR . '/' . $target;

                if (file_exists($full_path)) {
                    if (is_dir($full_path)) {
                        // 删除文件夹(递归)
                        $it = new RecursiveDirectoryIterator($full_path, RecursiveDirectoryIterator::SKIP_DOTS);
                        $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
                        foreach($files as $file) {
                            if ($file->isDir()){
                                rmdir($file->getRealPath());
                            } else {
                                unlink($file->getRealPath());
                            }
                        }
                        rmdir($full_path);
                    } else {
                        // 删除文件
                        unlink($full_path);
                    }
                    $deleted_count++;
                }
            }
            $message = "成功删除 {$deleted_count} 个项目!";
        } else {
            $error = "密码错误或未选择文件!";
        }
    }

    // 重命名文件或文件夹
    if (isset($_POST['rename'])) {
        $old_name = $_POST['old_name'];
        $new_name = $_POST['new_name'];

        if (!empty($new_name)) {
            $old_path = ($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR . '/' . $old_name;
            $new_path = ($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR . '/' . $new_name;

            if (file_exists($old_path) && !file_exists($new_path)) {
                rename($old_path, $new_path);
                $message = "重命名成功!";
            } else {
                $error = "重命名失败! 文件已存在或原文件不存在。";
            }
        }
    }
}

// 获取当前目录文件列表
$current_path = ($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR;
$all_files = [];
if (file_exists($current_path)) {
    $items = scandir($current_path);
    foreach ($items as $item) {
        if ($item !== '.' && $item !== '..') {
            $file_path = $current_path . '/' . $item;
            $all_files[] = [
                'name' => $item,
                'path' => $file_path,
                'size' => filesize($file_path),
                'type' => mime_content_type($file_path),
                'is_dir' => is_dir($file_path),
                'modified' => filemtime($file_path)
            ];
        }
    }
}

// 搜索功能
if (isset($_GET['search']) && !empty($_GET['search'])) {
    $search_term = strtolower($_GET['search']);
    $filtered_files = [];
    foreach ($all_files as $file) {
        if (strpos(strtolower($file['name']), $search_term) !== false) {
            $filtered_files[] = $file;
        }
    }
    $all_files = $filtered_files;
}

// 分页处理
$total_files = count($all_files);
$total_pages = ceil($total_files / $ITEMS_PER_PAGE);
$current_page = isset($_GET['page']) ? max(1, min($total_pages, intval($_GET['page']))) : 1;
$offset = ($current_page - 1) * $ITEMS_PER_PAGE;
$files = array_slice($all_files, $offset, $ITEMS_PER_PAGE);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>种子文件管理系统</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
            color: #333;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        .system-title {
            text-align: center;
            font-size: 28px;
            font-weight: bold;
            color: #2c3e50;
            margin: 20px 0;
            text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
        }
        .btn {
            display: inline-block;
            padding: 8px 16px;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s;
        }
        .btn:hover {
            background-color: #45a049;
        }
        .btn-danger {
            background-color: #f44336;
        }
        .btn-danger:hover {
            background-color: #d32f2f;
        }
        .btn-primary {
            background-color: #2196F3;
        }
        .btn-primary:hover {
            background-color: #0b7dda;
        }
        .btn-secondary {
            background-color: #757575;
        }
        .btn-secondary:hover {
            background-color: #616161;
        }
        .file-list {
            width: 100%;
            border-collapse: collapse;
        }
        .file-list th, .file-list td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        .file-list th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .file-list tr:hover {
            background-color: #f9f9f9;
        }
        .file-actions {
            display: flex;
            gap: 5px;
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
        }
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 500px;
            border-radius: 5px;
        }
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }
        .close:hover {
            color: black;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input, .form-group textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border: 1px solid transparent;
            border-radius: 4px;
        }
        .alert-success {
            color: #3c763d;
            background-color: #dff0d8;
            border-color: #d6e9c6;
        }
        .alert-danger {
            color: #a94442;
            background-color: #f2dede;
            border-color: #ebccd1;
        }
        .search-box {
            display: flex;
            gap: 10px;
        }
        .search-box input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .folder-icon {
            color: #FFA500;
            margin-right: 5px;
        }
        .file-icon {
            color: #2196F3;
            margin-right: 5px;
        }
        .breadcrumb {
            padding: 10px 0;
            margin-bottom: 15px;
        }
        .breadcrumb a {
            color: #2196F3;
            text-decoration: none;
        }
        .breadcrumb a:hover {
            text-decoration: underline;
        }
        .pagination {
            display: flex;
            justify-content: center;
            margin: 20px 0;
        }
        .pagination a {
            color: #2196F3;
            padding: 8px 16px;
            text-decoration: none;
            border: 1px solid #ddd;
            margin: 0 4px;
            border-radius: 4px;
        }
        .pagination a.active {
            background-color: #2196F3;
            color: white;
            border: 1px solid #2196F3;
        }
        .pagination a:hover:not(.active) {
            background-color: #ddd;
        }
        .batch-actions {
            margin: 15px 0;
            display: flex;
            gap: 10px;
        }
        .stats-container {
            background-color: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin: 20px 0;
            display: flex;
            justify-content: space-around;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .stat-item {
            text-align: center;
        }
        .stat-number {
            font-size: 24px;
            font-weight: bold;
            color: #2c3e50;
        }
        .stat-label {
            font-size: 14px;
            color: #7f8c8d;
        }
        .checkbox-cell {
            width: 30px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="system-title">种子文件管理系统</h1>

        <div class="header">
            <div>
                <a href="?dir=" class="btn btn-secondary">首页</a>
            </div>
            <div class="search-box">
                <form method="get" action="">
                    <input type="hidden" name="dir" value="<?php echo htmlspecialchars($CURRENT_DIR); ?>">
                    <input type="text" name="search" placeholder="搜索文件..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
                    <button type="submit" class="btn btn-primary">搜索</button>
                    <?php if (isset($_GET['search'])): ?>
                        <a href="?dir=<?php echo htmlspecialchars($CURRENT_DIR); ?>" class="btn btn-secondary">清除</a>
                    <?php endif; ?>
                </form>
            </div>
            <div>
                <button onclick="document.getElementById('uploadModal').style.display='block'" class="btn btn-primary">上传文件</button>
                <button onclick="document.getElementById('folderModal').style.display='block'" class="btn">新建文件夹</button>
            </div>
        </div>

        <?php if (isset($message)): ?>
            <div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
        <?php endif; ?>

        <?php if (isset($error)): ?>
            <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <!-- 面包屑导航 -->
        <div class="breadcrumb">
            <?php
            $path_parts = explode('/', $CURRENT_DIR);
            $current_path = '';
            echo '<a href="?dir=">根目录</a>';
            foreach ($path_parts as $part) {
                if (!empty($part)) {
                    $current_path .= '/' . $part;
                    echo ' / <a href="?dir=' . htmlspecialchars(ltrim($current_path, '/')) . '">' . htmlspecialchars($part) . '</a>';
                }
            }
            ?>
        </div>

        <!-- 统计信息 -->
        <div class="stats-container">
            <div class="stat-item">
                <div class="stat-number"><?php echo $total_files; ?></div>
                <div class="stat-label">总文件数</div>
            </div>
            <div class="stat-item">
                <div class="stat-number"><?php echo count(array_filter($all_files, function($f) { return $f['is_dir']; })); ?></div>
                <div class="stat-label">文件夹</div>
            </div>
            <div class="stat-item">
                <div class="stat-number"><?php echo count(array_filter($all_files, function($f) { return !$f['is_dir']; })); ?></div>
                <div class="stat-label">文件</div>
            </div>
            <div class="stat-item">
                <div class="stat-number"><?php echo $current_page; ?>/<?php echo max(1, $total_pages); ?></div>
                <div class="stat-label">当前页/总页数</div>
            </div>
        </div>

        <!-- 批量操作 -->
        <form method="post" id="batchForm">
            <input type="hidden" name="dir" value="<?php echo htmlspecialchars($CURRENT_DIR); ?>">
            <div class="batch-actions">
                <button type="button" onclick="selectAllFiles()" class="btn btn-secondary">全选</button>
                <button type="button" onclick="deselectAllFiles()" class="btn btn-secondary">取消全选</button>
                <button type="button" onclick="showBatchDeleteModal()" class="btn btn-danger">批量删除</button>
            </div>

            <table class="file-list">
                <thead>
                    <tr>
                        <th class="checkbox-cell"><input type="checkbox" id="selectAll"></th>
                        <th>名称</th>
                        <th>类型</th>
                        <th>大小</th>
                        <th>修改日期</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($files)): ?>
                        <tr>
                            <td colspan="6" style="text-align: center;">没有文件</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($files as $file): ?>
                            <tr>
                                <td><input type="checkbox" name="selected_files[]" value="<?php echo htmlspecialchars($file['name']); ?>"></td>
                                <td>
                                    <?php if ($file['is_dir']): ?>
                                        <i class="folder-icon">📁</i>
                                        <a href="?dir=<?php echo htmlspecialchars($CURRENT_DIR ? $CURRENT_DIR . '/' . $file['name'] : $file['name']); ?>">
                                            <?php echo htmlspecialchars($file['name']); ?>
                                        </a>
                                    <?php else: ?>
                                        <i class="file-icon">📄</i>
                                        <?php echo htmlspecialchars($file['name']); ?>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $file['is_dir'] ? '文件夹' : htmlspecialchars($file['type']); ?></td>
                                <td><?php echo $file['is_dir'] ? '-' : format_size($file['size']); ?></td>
                                <td><?php echo date('Y-m-d H:i:s', $file['modified']); ?></td>
                                <td class="file-actions">
                                    <?php if ($file['is_dir']): ?>
                                        <button onclick="showRenameModal('<?php echo htmlspecialchars($file['name']); ?>')" class="btn btn-secondary">重命名</button>
                                        <button onclick="showDeleteModal('<?php echo htmlspecialchars($file['name']); ?>', true)" class="btn btn-danger">删除</button>
                                    <?php else: ?>
                                        <a href="<?php echo htmlspecialchars(($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR . '/' . $file['name']); ?>" class="btn btn-primary" download>下载</a>
                                        <button onclick="showShareModal('<?php echo htmlspecialchars(($CURRENT_DIR ? $CURRENT_DIR . '/' : '') . $UPLOAD_DIR . '/' . $file['name']); ?>')" class="btn">分享</button>
                                        <button onclick="showRenameModal('<?php echo htmlspecialchars($file['name']); ?>')" class="btn btn-secondary">重命名</button>
                                        <button onclick="showDeleteModal('<?php echo htmlspecialchars($file['name']); ?>', false)" class="btn btn-danger">删除</button>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </form>

        <!-- 分页导航 -->
        <?php if ($total_pages > 1): ?>
        <div class="pagination">
            <?php if ($current_page > 1): ?>
                <a href="?dir=<?php echo htmlspecialchars($CURRENT_DIR); ?>&page=1<?php echo isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : ''; ?>">« 第一页</a>
                <a href="?dir=<?php echo htmlspecialchars($CURRENT_DIR); ?>&page=<?php echo $current_page - 1; ?><?php echo isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : ''; ?>">‹ 上一页</a>
            <?php endif; ?>

            <?php 
            // 显示页码,最多显示5个
            $start_page = max(1, $current_page - 2);
            $end_page = min($total_pages, $current_page + 2);

            if ($start_page > 1) {
                echo '<a href="?dir=' . htmlspecialchars($CURRENT_DIR) . '&page=1' . (isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : '') . '">1</a>';
                if ($start_page > 2) echo '<span>...</span>';
            }

            for ($i = $start_page; $i <= $end_page; $i++) {
                if ($i == $current_page) {
                    echo '<a href="?dir=' . htmlspecialchars($CURRENT_DIR) . '&page=' . $i . (isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : '') . '" class="active">' . $i . '</a>';
                } else {
                    echo '<a href="?dir=' . htmlspecialchars($CURRENT_DIR) . '&page=' . $i . (isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : '') . '">' . $i . '</a>';
                }
            }

            if ($end_page < $total_pages) {
                if ($end_page < $total_pages - 1) echo '<span>...</span>';
                echo '<a href="?dir=' . htmlspecialchars($CURRENT_DIR) . '&page=' . $total_pages . (isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : '') . '">' . $total_pages . '</a>';
            }
            ?>

            <?php if ($current_page < $total_pages): ?>
                <a href="?dir=<?php echo htmlspecialchars($CURRENT_DIR); ?>&page=<?php echo $current_page + 1; ?><?php echo isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : ''; ?>">下一页 ›</a>
                <a href="?dir=<?php echo htmlspecialchars($CURRENT_DIR); ?>&page=<?php echo $total_pages; ?><?php echo isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : ''; ?>">最后一页 »</a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>

    <!-- 上传文件模态框 -->
    <div id="uploadModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="document.getElementById('uploadModal').style.display='none'">&times;</span>
            <h2>上传文件</h2>
            <form method="post" enctype="multipart/form-data">
                <input type="hidden" name="dir" value="<?php echo htmlspecialchars($CURRENT_DIR); ?>">
                <div class="form-group">
                    <label for="files">选择文件 (可多选):</label>
                    <input type="file" name="files[]" id="files" multiple required>
                </div>
                <div class="form-group">
                    <label for="upload_password">密码:</label>
                    <input type="password" name="upload_password" id="upload_password" required>
                </div>
                <button type="submit" name="upload" class="btn btn-primary">上传</button>
            </form>
        </div>
    </div>

    <!-- 新建文件夹模态框 -->
    <div id="folderModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="document.getElementById('folderModal').style.display='none'">&times;</span>
            <h2>新建文件夹</h2>
            <form method="post">
                <input type="hidden" name="dir" value="<?php echo htmlspecialchars($CURRENT_DIR); ?>">
                <div class="form-group">
                    <label for="folder_name">文件夹名称:</label>
                    <input type="text" name="folder_name" id="folder_name" required>
                </div>
                <button type="submit" name="create_folder" class="btn btn-primary">创建</button>
            </form>
        </div>
    </div>

    <!-- 删除确认模态框 -->
    <div id="deleteModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="document.getElementById('deleteModal').style.display='none'">&times;</span>
            <h2>确认删除</h2>
            <form method="post">
                <input type="hidden" name="dir" value="<?php echo htmlspecialchars($CURRENT_DIR); ?>">
                <input type="hidden" name="target" id="delete_target" value="">
                <div class="form-group">
                    <p id="delete_message"></p>
                    <label for="delete_password">密码:</label>
                    <input type="password" name="delete_password" id="delete_password" required>
                </div>
                <button type="submit" name="delete" class="btn btn-danger">确认删除</button>
            </form>
        </div>
    </div>

    <!-- 批量删除确认模态框 -->
    <div id="batchDeleteModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="document.getElementById('batchDeleteModal').style.display='none'">&times;</span>
            <h2>确认批量删除</h2>
            <form method="post" id="batchDeleteForm">
                <input type="hidden" name="dir" value="<?php echo htmlspecialchars($CURRENT_DIR); ?>">
                <div class="form-group">
                    <p>确定要删除选中的项目吗?</p>
                    <label for="batch_delete_password">密码:</label>
                    <input type="password" name="batch_delete_password" id="batch_delete_password" required>
                </div>
                <button type="submit" name="batch_delete" class="btn btn-danger">确认删除</button>
            </form>
        </div>
    </div>

    <!-- 重命名模态框 -->
    <div id="renameModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="document.getElementById('renameModal').style.display='none'">&times;</span>
            <h2>重命名</h2>
            <form method="post">
                <input type="hidden" name="dir" value="<?php echo htmlspecialchars($CURRENT_DIR); ?>">
                <input type="hidden" name="old_name" id="old_name" value="">
                <div class="form-group">
                    <label for="new_name">新名称:</label>
                    <input type="text" name="new_name" id="new_name" required>
                </div>
                <button type="submit" name="rename" class="btn btn-primary">重命名</button>
            </form>
        </div>
    </div>

    <!-- 分享模态框 -->
    <div id="shareModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="document.getElementById('shareModal').style.display='none'">&times;</span>
            <h2>分享文件</h2>
            <div class="form-group">
                <label>分享链接:</label>
                <input type="text" id="share_link" readonly>
                <button onclick="copyShareLink()" class="btn btn-primary">复制链接</button>
            </div>
        </div>
    </div>

    <script>
        // 显示删除确认模态框
        function showDeleteModal(target, isFolder) {
            document.getElementById('delete_target').value = target;
            document.getElementById('delete_message').textContent = '确定要删除 ' + target + (isFolder ? ' 文件夹及其所有内容吗?' : ' 文件吗?');
            document.getElementById('deleteModal').style.display = 'block';
        }

        // 显示批量删除确认模态框
        function showBatchDeleteModal() {
            var selectedCount = document.querySelectorAll('input[name="selected_files[]"]:checked').length;
            if (selectedCount > 0) {
                document.getElementById('batchDeleteModal').style.display = 'block';
            } else {
                alert('请至少选择一个文件或文件夹');
            }
        }

        // 显示重命名模态框
        function showRenameModal(oldName) {
            document.getElementById('old_name').value = oldName;
            document.getElementById('new_name').value = oldName;
            document.getElementById('renameModal').style.display = 'block';
        }

        // 显示分享模态框
        function showShareModal(filePath) {
            var shareLink = window.location.origin + window.location.pathname.replace(/[^/]*$/, '') + filePath;
            document.getElementById('share_link').value = shareLink;
            document.getElementById('shareModal').style.display = 'block';
        }

        // 复制分享链接
        function copyShareLink() {
            var copyText = document.getElementById('share_link');
            copyText.select();
            document.execCommand('copy');
            alert('链接已复制到剪贴板!');
        }

        // 全选文件
        function selectAllFiles() {
            var checkboxes = document.querySelectorAll('input[name="selected_files[]"]');
            checkboxes.forEach(function(checkbox) {
                checkbox.checked = true;
            });
            document.getElementById('selectAll').checked = true;
        }

        // 取消全选
        function deselectAllFiles() {
            var checkboxes = document.querySelectorAll('input[name="selected_files[]"]');
            checkboxes.forEach(function(checkbox) {
                checkbox.checked = false;
            });
            document.getElementById('selectAll').checked = false;
        }

        // 点击模态框外部关闭模态框
        window.onclick = function(event) {
            if (event.target.className === 'modal') {
                event.target.style.display = 'none';
            }
        }

        // 全选/取消全选
        document.getElementById('selectAll').addEventListener('change', function() {
            var checkboxes = document.querySelectorAll('input[name="selected_files[]"]');
            checkboxes.forEach(function(checkbox) {
                checkbox.checked = this.checked;
            }, this);
        });
    </script>
</body>
</html>

<?php
// 格式化文件大小
function format_size($size) {
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    $i = 0;
    while ($size >= 1024 && $i < count($units) - 1) {
        $size /= 1024;
        $i++;
    }
    return round($size, 2) . ' ' . $units[$i];
}
?>
×

如果觉得文章对您有用,请随意打赏。
您的支持是我们继续创作的动力!

微信打赏

微信扫一扫

支付宝打赏

支付宝扫一扫

扫描二维码,在手机上阅读

评论一下?

OωO
取消