目录
音乐列表
音乐上传
<?php
require_once '../mysql_demo1.php';
if (empty($mysqlconnect)) {
exit('数据库连接失败');
}
$querylist = mysqli_query($mysqlconnect,'select * from music_tb');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="./bootstrap.css">
</head>
<body>
<div class="container mt-4 text-center">
<h1 class="display-4">音乐列表</h1>
<div class="mb-3">
<a href="add.php" class="btn btn-secondary btn-sm">添加</a>
</div>
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>编号</th>
<th>标题</th>
<th>歌手</th>
<th>音乐</th>
<th>海报</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php while ( $row = mysqli_fetch_assoc($querylist )) {?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["title"]; ?></td>
<td><?php echo $row["artist"]; ?></td>
<td><img src="<?php echo $row['source']; ?>" alt="" width="200px" height="100px"></td>
<td><audio src="<?php echo $row['images']; ?>" controls></audio></td>
<td><a class="btn btn-danger btn-sm" href="remove.php?id=<?php echo $row["id"]; ?>">删除</a></td>
</tr>
<?php } ?>
</table>
</div>
<?php //释放结果集
mysqli_free_result($querylist);
//关闭连接
mysqli_close($mysqlconnect); ?>
</body>
</html>
------音乐上传功能
<?php
function addmusic( ){
require_once '../mysql_demo1.php';
if (empty($mysqlconnect)) {
exit('连接数据库失败');
}
var_dump($_FILES);
if (empty($_POST["title"])) {
$GLOBALS['message_error'] = '请输入标题';
return ;
}
if (empty($_POST['artist'])) {
$GLOBALS['message_error'] = '请输入歌手';
return ;
}
//校检文件上传
if (empty($_FILES['source'])) {
//表单照片中没有雨source文件域
$GLOBALS['message_error'] = '请正确上传音频文件';
return;
}
$source = $_FILES['source'];
//判断用户是否选择了文件
if ($source['error'] !== UPLOAD_ERR_OK) {
$GLOBALS['message_error'] = '请选择音乐文件';
return ;
}
//限制音频文件前大小
if ($source['size'] > 10 * 1024 *1024) {
$GLOBALS['message_error'] = '音频文件过大';
return ;
}
if ($source['size'] < 1 * 1024 *1024) {
$GLOBALS['message_error'] = '音频文件太小';
return ;
}
//类型校验
$sourcename = array('audio/mp3' ,'audio/wma' );
if (!in_array($source['type'], $sourcename)) {
$GLOBALS['message_error'] = '音频文件文件类型不和法';
return ;
}
$musictarget = './uploads/'.uniqid().'-'.$source['name'];
//移动音频文件
if (!move_uploaded_file($source['tmp_name'],$musictarget )) {
$GLOBALS['message_error'] = '请选择英语文件';
return ;
}
//校检海报上传
if (empty($_FILES['images'])) {
$GLOBALS['message_error'] = '请正确上传图片文件';
return;
}
$images = $_FILES['images'];
$imagespath = array();
//类型校验
for ($i=0; $i < count($images['name']); $i++) {
if ($images['error'][$i] !== UPLOAD_ERR_OK) {
$GLOBALS['message_error'] = '上传海报文件失败1';
return;
}
if ($images['size'][$i] > 4* 1024 *1024) {
$GLOBALS['message_error'] = '图片文件过大';
return ;
}
if (strpos($images['type'][$i],'image/') !== 0) {
$GLOBALS['message_error'] = '文件类型不和法';
return ;
}
$imagetarget = './uploads/'.uniqid().'-'. $images['name'][$i];
if (!move_uploaded_file($images['tmp_name'][$i],$imagetarget)) {
$GLOBALS['message_error'] = '上传图片失败';
return ;
}
$imagespath[$i] = $imagetarget;
}
$title = $_POST['title'];
$artist = $_POST['artist'];
$sourcepath = $musictarget;
$sql = "INSERT INTO `android_db`.`music_tb` (`id`, `title`, `artist`, `images`, `source`) VALUES ('".uniqid()."','".$title."', '".$artist."', '". $sourcepath."', '".implode(',',$imagespath)."')";
mysqli_query($mysqlconnect,$sql);
$row = mysqli_affected_rows($mysqlconnect);
mysqli_close($mysqlconnect);
header("location: musiclist.php");
$GLOBALS['message_error'] = '添加音乐失败';
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
addmusic();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加新音乐</title>
<link rel="stylesheet" href="bootstrap.css">
</head>
<body>
<div class="container py-5">
<h1 class="display-4">添加新音乐</h1>
<hr>
<?php if (isset($message_error)): ?>
<div class="alert alert-danger" role="alert">
<?php echo $message_error; ?></div>
<?php endif ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" autocomplete="off" >
<div class="form-group">
<label for="title">标题</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group">
<label for="artist">歌手</label>
<input type="text" class="form-control" id="artist" name="artist">
</div>
<div class="form-group">
<label for="images">海报</label>
<!-- 文件域 多选 multiple -->
<input type="file" class="form-control" id="images" name="images[]" accept="image/*" multiple="multiple">
</div>
<div class="form-group">
<label for="source">音乐</label>
<!--限制文件上传种类 accept="audio/*"-->
<input type="file" class="form-control" id="source" name="source" accept="audio/*">
</div>
<button class="btn btn-primary btn-block">保存</button>
</form>
</div>
</body>
</html>
------删除音乐
<?php
require_once '../mysql_demo1.php';
if (empty($mysqlconnect)) {
exit('数据库连接失败');
}
if (empty($_GET['id'])) {
exit ("<h1>请传递指定餐宿</h1>");
}
$id = $_GET['id'];
mysqli_query($mysqlconnect,"delete from music_tb where id = '".$id."'");
$row = mysqli_affected_rows($mysqlconnect);
if( $row > 0){
mysqli_close($mysqlconnect);
header('location:musiclist.php');
}
?>