php音乐列表
<?php
function add_music()
{
$data = array();
$data['id'] = uniqid();
if (empty($_POST['title'])) {
$GLOBALS['msg'] = '请输入标题';
return;
}
if (empty($_POST['singer'])) {
$GLOBALS['msg'] = '请输入歌手名';
return;
}
$data['title'] = $_POST['title'];
$data['singer'] = $_POST['singer'];
if (!isset($_FILES['music'])) {
$GLOBALS['msg'] = '请上传音乐文件';
return;
}
$music = $_FILES['music'];
if ($music['error'] !== UPLOAD_ERR_OK) {
$GLOBALS['msg'] = '上传音乐文件失败1';
return;
}
if ($music['size'] > 10 * 1024 * 1024) {
$GLOBALS['msg'] = '上传音乐文件超过10M';
return;
}
if ($music['size'] < 1 * 1024 * 1024) {
$GLOBALS['msg'] = '上传音乐文件小于1M';
return;
}
$allow_music = array('audio/mp3', 'audio/wma', 'audio/mpeg');
if (!in_array($music['type'], $allow_music)) {
$GLOBALS['msg'] = '不支持的音乐格式';
return;
}
$source = $music['tmp_name'];
$target1 = "./uploads/" . uniqid() . "-" . $music['name'];
if (!move_uploaded_file($source, $target1)) {
$GLOBALS['msg'] = '上传音乐文件失败2';
return;
}
$data['source'] = $target1;
if (!isset($_FILES['images'])) {
$GLOBALS['msg'] = '哎呀别闹';
return;
}
$images = $_FILES['images'];
$data['images'] = array();
for ($i = 0; $i < count($images['error']); $i++) {
if ($images['error'][$i] !== UPLOAD_ERR_OK) {
$GLOBALS['msg'] = '图片上传失败1';
return;
}
if (strpos($images['type'][$i], 'image/') !== 0) {
$GLOBALS['msg'] = '不支持的图片格式';
return;
}
$source = $images['tmp_name'][$i];
$target2 = "./uploads/" . uniqid() . '-' . $images['name'][$i];
if (!move_uploaded_file($source, $target2)) {
$GLOBALS['msg'] = '图片上传失败2';
return;
}
$data['images'][] = $target2;
}
$arr = json_decode(file_get_contents('music.json'), true);
array_push($arr, $data);
file_put_contents('music.json', json_encode($arr));
header('Location: list.php');
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
add_music();
}; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="bootstrap.css">
<title>添加音乐</title>
</head>
<body>
<div class="container">
<h1 class="display-4 mt-5 mb-4">添加音乐</h1>
<hr>
<?php if (isset($msg)) : ?>
<div class="alert alert-danger" role="alert">
<?php echo $msg; ?>
</div>
<?php endif; ?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method='post' enctype="multipart/form-data" class="form-group">
<div class="mb-3">
<label for="title">标题</label>
<input type="text" id="title" name="title" class="form-control" placeholder="请输入标题" </div> <div class="mb-3 mt-3">
<label for="singer">歌手</label>
<input type="text" id="singer" name="singer" class="form-control" placeholder="请输入歌手">
</div>
<div class="mb-3">
<label for="images">海报</label>
<!-- accept: 它规定能够通过文件上传进行提交的文件类型 -->
<!-- multiple: 按ctrl配合鼠标左键选中多个文件 -->
<input type="file" id="images" name="images[]" class="form-control" accept="image/*" multiple />
</div>
<div class="mb-4">
<label for="music">音乐</label>
<input type="file" id="music" name="music" class="form-control" accept="audio/*">
</div>
<button class="btn btn-lg btn-primary btn-block">保存</button>
</form>
</div>
</body>
</html>
<?php
if(empty($_GET['id'])){
exit('<h1>哎呀别闹</h1>');
}
$id = $_GET['id'];
$data = json_decode(file_get_contents('music.json'),true);
foreach ($data as $key) {
if($key['id'] !== $id) continue;
$index = array_search($key, $data);
array_splice($data, $index, 1);
file_put_contents('music.json',json_encode($data));
}
header('Location:list.php');
; ?>
<?php
$contents = file_get_contents('music.json');
$arr = json_decode($contents, true);
; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>音乐列表</title>
<link rel="stylesheet" href="bootstrap.css">
</head>
<body>
<div class="container">
<h1 class="display-4 mt-5">音乐列表</h1>
<hr>
<a href="add.php" class="btn btn-primary mb-2">添加</a>
<table class="table table-hover table-bordered">
<thead class="thead-dark" style="text-align:center">
<th>标题</th>
<th>歌手</th>
<th>海报</th>
<th>音乐</th>
<th>操作</th>
</thead>
<tbody>
<?php foreach ($arr as $item) : ?>
<tr>
<td class="align-middle"><?php echo $item['title'] ?></td>
<td class="align-middle"><?php echo $item['singer'] ?></td>
<td class="align-middle">
<?php foreach ($item['images'] as $img) : ?>
<img src="<?php echo $img ?>" alt="">
<?php endforeach ?>
</td>
<td class="align-middle"><audio src="<?php echo $item['source'] ?>" controls></audio></td>
<td class="align-middle">
<!-- <div class="btn btn-danger btn-small">删除</div> -->
<a class="btn btn-danger btn-small" href="delete.php?id=<?php echo $item['id']?>">删除</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</body>
</html>