<?php // 设置上传目录 $upload_dir = __DIR__ . '/bbs_data/1/'; // 检查上传目录是否存在,不存在则创建 if (!is_dir($upload_dir)) { mkdir($upload_dir, 0777, true); } // 获取当前日期并创建对应的子目录 $year = date('Y'); $month = date('m'); $day = date('d'); $sub_dir = $upload_dir . $year . '/' . $month . '/' . $day; if (!is_dir($sub_dir)) { mkdir($sub_dir, 0777, true); } // 处理文件上传 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['files'])) { $files = $_FILES['files']; $file_count = count($files['name']); $uploaded_files = []; // 用于存储上传文件的直链 for ($i = 0; $i < $file_count; $i++) { $file_name = $files['name'][$i]; $file_tmp = $files['tmp_name'][$i]; $file_size = $files['size'][$i]; $file_error = $files['error'][$i]; // 获取文件扩展名 $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); // 生成唯一的文件名 $file_prefix = date('YmdHis') . '_'; // 使用当前时间的时间戳 $file_suffix = 1; $new_file_name = $file_prefix . $file_suffix . '.' . $file_ext; $destination = $sub_dir . '/' . $new_file_name; // 检查文件大小和错误 if ($file_size > 5 * 1024 * 1024) { // 限制文件大小为5MB continue; } if ($file_error !== UPLOAD_ERR_OK) { continue; } // 检查文件是否已存在,如果存在则修改文件名 while (file_exists($destination)) { $file_suffix++; $new_file_name = $file_prefix . $file_suffix . '.' . $file_ext; $destination = $sub_dir . '/' . $new_file_name; } // 移动文件到目标位置 if (move_uploaded_file($file_tmp, $destination)) { // 文件上传成功 // 将文件直链添加到数组 $uploaded_files[] = $new_file_name; } } } ?> <!DOCTYPE html> <html lang="en"> <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; margin: 0; padding: 0; display: flex; flex-direction: column; align-items: center; height: 100vh; background-color: #f4f4f4; } .upload-container { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); text-align: center; width: 80%; max-width: 600px; margin-bottom: 20px; } .upload-container h1 { margin-bottom: 20px; } .upload-container input[type="file"] { margin-top: 20px; } #uploaded-files { padding: 10px; background: #fff; border: 1px solid #ddd; border-radius: 4px; width: 80%; max-width: 600px; } a { color: #0645ad; text-decoration: none; } a:hover { text-decoration: underline; } </style> </head> <body> <div class="upload-container"> <h1>上传文件</h1> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="files[]" multiple required> <button type="submit">上传</button> </form> </div> <?php if (!empty($uploaded_files)): ?> <div id="uploaded-files"> <h2>已上传文件直链:</h2> <ul> <?php foreach ($uploaded_files as $file): ?> <li><a href="<?php echo '/bbs_data/1/' . $year . '/' . $month . '/' . $day . '/' . $file; ?>" target="_blank"><?php echo $file; ?></a></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> </body> </html>
请求资源或报告无效资源,请点击[反馈中心]