一、详细介绍
一款在线多人实时聊天室系统,支持表情包、图片、视频发送,支持每次发送消息随机用户名,无需数据库,无后台
测试环境:PHP7.4
更新:
修复视频无法上传问题
--------------------搭建教程--------------------
1.新建站点
2.上传源码到网站根目录解压
3.访问域名即可进入前台
首页消息记录在chat_data.json文件里可清理
消息刷新时间在index.php文件第663行可设置,15000等于15秒
二、效果展示
1.部分代码
代码如下(示例):
function generateRandomUsername() {
$adjectives = ['快乐', '聪明', '勇敢', '安静', '活泼', '神秘', '幽默', '勤奋'];
$animals = ['熊猫', '老虎', '兔子', '龙', '凤凰', '鲸鱼', '狐狸', '猫咪'];
return $adjectives[array_rand($adjectives)] . $animals[array_rand($animals)] . rand(100, 999);
}
// 处理文件上传
function handleFileUpload($field) {
if (!isset($_FILES[$field]) || $_FILES[$field]['error'] !== UPLOAD_ERR_OK) {
return ['error' => '文件上传错误'];
}
$file = $_FILES[$field];
// 扩展的MIME类型列表
$allowedTypes = [
'image' => ['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
'video' => ['video/mp4', 'video/mpeg', 'application/mp4', 'video/x-m4v', 'video/quicktime', 'video/webm', 'video/ogg']
];
// 根据扩展名先判断类型
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$videoExt = ['mp4', 'webm', 'ogg', 'mov', 'm4v'];
$imageExt = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
$type = in_array($extension, $videoExt) ? 'video' :
(in_array($extension, $imageExt) ? 'image' : null);
if (!$type) {
return ['error' => '不支持的文件类型:' . $extension];
}
$maxSize = $type === 'image' ? 5 * 1024 * 1024 : 50 * 1024 * 1024;
// 验证实际MIME类型
$detectedMime = mime_content_type($file['tmp_name']);
if (!in_array($detectedMime, $allowedTypes[$type])) {
return ['error' => '不支持的文件格式(MIME: '.$detectedMime.')'];
}
if ($file['size'] > $maxSize) {
return ['error' => '文件大小超过限制'];
}
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = uniqid() . '.' . preg_replace('/[^a-z0-9]/i', '', $extension);
$targetPath = 'uploads/' . $filename;
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
return ['path' => $targetPath, 'type' => $type];
}
return ['error' => '文件保存失败'];
}
// 处理OPTIONS预检请求
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
2.效果图展示

1665

被折叠的 条评论
为什么被折叠?



