php+vue3实现简洁大气的网盘云盘文件管理系统代码

代码语言:php

所属分类:文件

代码描述:php+vue3实现简洁大气的网盘云盘文件管理系统代码,可上传文件单个或多个、可上传目录、可批量删除,还可在线预览播放视频、图片、声音。

代码标签: php vue 简洁 大气 网盘 云盘 文件 管理 系统 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
// ##################################################################
// #            PHP & Vue3 单文件网盘管理系统                      #
// #            作者: 专业PHP/HTML全栈工程师                        #
// #            版本: 1.1 (支持文件和文件夹分别上传)                #
// ##################################################################

// --- 配置区 ---
// 设置你希望管理的根目录的绝对路径,结尾不要加'/'
$root_dir = '/var/www/html/files'; // <-- Linux 示例: /var/www/my_files
// $root_dir = 'C:/www/files';    // <-- Windows 示例: C:/www/files

// 身份验证 (可选, 简单的密码保护)
// 留空则禁用密码: $password = '';
$password = 'admin'; // 在这里设置你的密码
// --- 配置区结束 ---


// --- 后端逻辑处理 ---
session_start();

function check_auth() {
    global $password;
    if (!empty($password) && (!isset($_SESSION['is_logged_in']) || $_SESSION['is_logged_in'] !== true)) {
        return false;
    }
    return true;
}

function handle_login() {
    global $password;
    if (isset($_POST['password']) && $_POST['password'] === $password) {
        $_SESSION['is_logged_in'] = true;
        header('Location: ' . $_SERVER['PHP_SELF']);
        exit;
    }
    return '密码错误';
}

if (!empty($password)) {
    if (isset($_GET['action']) && $_GET['action'] == 'logout') {
        session_destroy();
        header('Location: ' . $_SERVER['PHP_SELF']);
        exit;
    }
    if (!check_auth()) {
        if (isset($_POST['password'])) {
            $login_error = handle_login();
        }
        // 显示登录页面
        header('Content-Type: text/html; charset=utf-8');
        echo <<<HTML
<!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: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f0f2f5; margin: 0; }
        .login-container { background: #fff; padding: 40px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; }
        h2 { color: #333; margin-bottom: 20px; }
        input { width: 100%; padding: 12px; margin-bottom: 20px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; }
        button { width: 100%; padding: 12px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
        button:hover { background-color: #0056b3; }
        .error { color: #dc3545; margin-top: -10px; margin-bottom: 10px; }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>系统登录</h2>
        <form method="post">
            <input type="password" name="password" placeholder="请输入密码" required>
            <button type="submit">登录</button>
        </form>
        
HTML;
        if (isset($login_error)) {
            echo '<p class="error">' . htmlspecialchars($login_error) . '</p>';
        }
        echo '</div></body></html>';
        exit;
    }
}


$action = $_GET['action'] ?? '';

if (!empty($action)) {
    // API请求,关闭HTML输出
    error_reporting(0);
    header('Content-Type: application/json; charset=utf-8');

    // 安全函数:确保路径在根目录内
    function get_safe_path($path) {
        global $root_dir;
        // 移除路径中的 '..'
        $path = str_replace('..', '', $path);
        // 拼接成绝对路径
        $full_path = $root_dir . $path;
        // 获取真实路径
        $real_path = realpath($full_path);
        // 检查路径是否以根目录开头,并且是真实存在的路径
        if ($real_path === false || strpos($real_path, realpath($root_dir)) !== 0) {
            // 如果是创建目录,允许路径不存在但父目录必须在根目录内
            $parent_real_path = realpath(dirname($full_path));
            if ($parent_real_path !== false && strpos($parent_real_path, realpath($root_dir)) === 0) {
                 return $full_path;
            }
            return false;
        }
        return $real_path;
    }
    
    // 递归删除目录
    function delete_recursive($dir) {
        if (!file_exists($dir)) return true;
        if (!is_dir($dir)) return unlink($dir);
        foreach (scandir($dir) as $item) {
            if ($item == '.' || $item == '..') continue;
            if (!delete_recursive($dir . DIRECTORY_SEPARATOR . $item)) return false;
        }
        return rmdir($dir);
    }

    // 格式化文件大小
    function format_size($bytes) {
        if ($bytes >= 1073741824) {
            return number_format($bytes / 1073741824, 2) . ' GB';
        } elseif ($bytes >= 1048576) {
            return number_format($bytes / 1048576, 2) . ' MB';
        } elseif ($bytes >= 1024) {
            return number_format($bytes / 1024, 2) . ' KB';
        } elseif ($bytes > 0) {
            return $bytes . ' B';
        } else {
            return '0 B';
        }
    }

    $path = $_GET['path'] ?? '/';
    $safe_path = get_safe_path($path);

    if ($safe_path === false && $action !== 'upload') { // upload action has special path handling
        echo json_encode(['success' => false, 'message' => '无效或不安全的路径']);
        exit;
    }

    switch ($action) {
        case 'list':
            $files = [];
            $items = scandir($safe_path);
            foreach ($items as $item) {
                if ($item === '.' || $item === '..') continue;
                $item_path = $safe_path . '/' . $item;
                $is_dir = is_dir($item_path);
                $files[] = [
                    'name' => $item,
                    'type' => $is_dir ? 'dir' : 'file',
                    'size' => $is_dir ? '' : format_size(filesize($item_path)),
                    'mtime' => date('Y-m-d H:i:s', filemtime($item_path)),
                ];
            }
            // 排序:文件夹在前,文件在后,按名称排序
            usort($files.........完整代码请登录后点击上方下载按钮下载查看

网友评论0