main
xgkblog/admin/media.php
media.php8.4 KB
<?php
require 'config.php';
$page_title = "媒体管理";

// 获取当前网站的基础 URL(自动识别 http/https 和域名)
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'];

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $uploadDir = '../uploads/';
    if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true);
    
    $fileName = time() . '_' . basename($_FILES['file']['name']);
    $filePath = $uploadDir . $fileName;
    
    if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) {
        $stmt = $pdo->prepare("INSERT INTO media (file_name, file_path, mime_type) VALUES (:name, :path, :mime)");
        $stmt->execute([
            ':name' => $_FILES['file']['name'],
            ':path' => '/uploads/' . $fileName,
            ':mime' => $_FILES['file']['type']
        ]);
    }
    header('Location: media.php?msg=uploaded'); exit;
}

$stmt = $pdo->query("SELECT * FROM media ORDER BY uploaded_at DESC");
$medias = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?></title>
    <style>
        /* 链接与复制按钮样式 */
        .file-link-wrapper {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .file-link-url {
            font-size: 12px;
            color: #666;
            max-width: 200px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .btn-copy {
            padding: 4px 8px;
            font-size: 12px;
            background: #f0f4f8;
            border: 1px solid #d1d9e6;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.2s;
            white-space: nowrap;
        }
        .btn-copy:hover {
            background: #4a90e2;
            color: #fff;
            border-color: #4a90e2;
        }
    </style>
</head>
<body>

    <div class="overlay" id="overlay"></div>

    <!-- 侧边栏 -->
    <?php require_once __DIR__ . '/views/sidebar.php'; ?>

    <!-- 主内容区 -->
    <div class="main-wrapper">
        <?php require_once __DIR__ . '/views/header.php'; ?>
        <main class="content-area">
            <div class="page-header">
                <h1 class="page-title">媒体管理</h1>
                <button class="btn-primary"><a href="post_edit.php">+ 撰写新文章</a></button>
            </div>
            
            <!-- 统计卡片 -->
            <?php require_once __DIR__ . '/views/tj.php'; ?>

            <!-- 提示信息 -->
            <?php if (!empty($_GET['msg'])): ?>
                <p style="color:green;font-weight:bold;margin-bottom:15px;">✅ 操作成功!</p>
            <?php endif; ?>

            <!-- ================= 1. 媒体上传表单 ================= -->
            <div class="card" style="margin-bottom: 30px;">
                <h3 style="margin:0 0 20px 0; font-size:18px;">📤 上传新媒体文件</h3>
                <form method="POST" enctype="multipart/form-data">
                    <div style="margin-bottom: 15px;">
                        <label style="display:block; margin-bottom:5px; font-weight:bold;">选择文件</label>
                        <input type="file" name="file" class="form-control" required>
                    </div>
                    <div>
                        <button type="submit" class="btn btn-primary">上传文件</button>
                    </div>
                </form>
            </div>

            <!-- ================= 2. 媒体列表表格 ================= -->
            <div class="card" style="padding: 0; overflow: hidden;">
                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th style="width: 40px;"><input type="checkbox" class="table-checkbox"></th>
                                <th style="width: 60px;">ID</th>
                                <th>预览</th>
                                <th>文件名</th>
                                <th>类型</th>
                                <th>文件链接 (可复制)</th> <!-- 新增列 -->
                                <th>上传时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($medias)): ?>
                                <?php foreach ($medias as $media): ?>
                                <?php 
                                    // 拼接完整的文件 URL
                                    $full_url = $base_url . htmlspecialchars($media['file_path']); 
                                ?>
                                <tr>
                                    <td><input type="checkbox" class="table-checkbox"></td>
                                    <td><?php echo $media['id']; ?></td>
                                    <td>
                                        <?php if(strpos($media['mime_type'], 'image') !== false): ?>
                                            <img src="<?php echo htmlspecialchars($media['file_path']); ?>" 
                                                 width="50" height="50" 
                                                 style="object-fit:cover; border-radius:4px; border:1px solid #eee;" 
                                                 alt="预览">
                                        <?php else: ?>
                                            <span style="font-size:24px;">📄</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <div class="article-title"><?php echo htmlspecialchars($media['file_name']); ?></div>
                                    </td>
                                    <td>
                                        <div class="article-cat"><?php echo htmlspecialchars($media['mime_type']); ?></div>
                                    </td>
                                    <!-- 新增:文件链接与复制按钮 -->
                                    <td>
                                        <div class="file-link-wrapper">
                                            <span class="file-link-url" title="<?php echo $full_url; ?>"><?php echo $full_url; ?></span>
                                            <button class="btn-copy" onclick="copyLink(this, '<?php echo $full_url; ?>')">📋 复制</button>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="article-cat"><?php echo date('Y-m-d H:i', strtotime($media['uploaded_at'])); ?></div>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="7" style="text-align:center; color:#999; padding:30px 0;">暂无媒体文件,请在上方上传。</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
          
        </main>
    </div>

    <?php require_once __DIR__ . '/views/footer.php'; ?>

    <!-- 一键复制 JS 逻辑 -->
    <script>
        function copyLink(btn, url) {
            // 使用现代 Clipboard API 复制文本
            navigator.clipboard.writeText(url).then(function() {
                const originalText = btn.innerText;
                btn.innerText = '✅ 已复制';
                btn.style.background = '#1e7e34';
                btn.style.color = '#fff';
                btn.style.borderColor = '#1e7e34';
                
                // 2秒后恢复按钮原状
                setTimeout(function() {
                    btn.innerText = originalText;
                    btn.style.background = '';
                    btn.style.color = '';
                    btn.style.borderColor = '';
                }, 2000);
            }).catch(function(err) {
                alert('复制失败,请手动复制!');
            });
        }
    </script>
</body>
</html>