小小项目:网页音乐播放器

项目名称:网页音乐播放器
使用技术:该项目的服务端是基于Apache的,数据保存的格式为json
项目描述:在网页上播放服务端的音乐,并能实现音乐的上传和删除,其中的CSS样式使用到bootstrap,服务端的语言为PHP

第一步先做好音乐列表的样式,样式如下图:
图片为音乐列表源代码list.php如下:

<!-- 
date:2019-8-1 
name:小鱼 
主页面:list.php
-->

<?php 
    //TODO: 获取到json格式的文件内容,其中的内容是以字符串的形式存放在str中
	$str = file_get_contents('D:\apache\site1\songs\data.json');
	// var_dump($str);
	
	//TODO: 对字符串进行解码,true参数表示解码后为数组形式的数据
	$data = json_decode($str,true);
	// var_dump($data);
	// array(2) {
 //  [0]=>
 //  array(5) {
 //    ["id"]=>
 //    string(13) "5d429077480e1"
 //    ["title"]=>
 //    string(9) "陈奕迅"
 //    ["artist"]=>
 //    string(6) "单车"
 //    ["images"]=>
 //    array(2) {
 //      [0]=>
 //      string(46) "/unload/5d429077480e9-59f2d87f42a57icon-01.png"
 //      [1]=>
 //      string(46) "/unload/5d42907748979-59f2d87f43ecficon-03.png"
 //    }
 //    ["source"]=>
 //    string(46) "/unload/5d42907757919-4.陈奕迅 - 单车.mp3"
 //  }
 //  [1]=>
 //  array(5) {
 //    ["id"]=>
 //    string(13) "5d42973e9e15d"
 //    ["title"]=>
 //    string(6) "十年"
 //    ["artist"]=>
 //    string(9) "陈奕迅"
 //    ["images"]=>
 //    array(1) {
 //      [0]=>
 //      string(46) "/unload/5d42973e9e165-59f2d87f43511icon-02.png"
 //    }
 //    ["source"]=>
 //    string(47) "/unload/5d42973ea3fe0-11.陈奕迅 - 十年.mp3"
 //  }
 ?>

<!DOCTYPE html>
<html lang="zh">
<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>
	<!-- 连接bootstrap.css样式库
	<link rel="stylesheet" href="../css/bootstrap.css">
</head>
<body>
	<div class="container">
		<h3>音乐列表</h3>
		<hr />
		<!-- 跳转的add.php页面 -->
		<a href="add.php" type="button" class="btn btn-primary">添加</a>
		<table class="table table-bordered table-hover table-striped">
			<thead>
				<tr>
					<th>歌名</th>
					<th>作者</th>
					<th>海报</th>
					<th>歌曲</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				<!-- 遍历data数据 -->
				<?php foreach($data as $item) {?>
					<tr>
						<td><?php echo $item["title"] ?></td>
						<td><?php echo $item["artist"] ?></td>
						<td>
							<!-- 如果有多张海报则循环遍历海报并显示 -->
							<?php for($i=0; $i < count($item["images"]); $i++) {?>
								<img src="<?php echo $item["images"][$i] ?>" alt="">
							<?php }?>							
						</td>
						<td><audio src="<?php echo $item["source"] ?>" controls></audio></td>
						<!-- 点击删除键会跳转到delete.php进行删除操作,完成后会返回到主页面list.php -->
						<td><a class="btn btn-danger" href="delete.php?id=<?php echo $item['id']; ?>">删除</a></td>
					</tr>
				<?php }?>		
			</tbody>
		</table>
		
	</div>
</body>
</html>

第二步:添加歌曲的模块
其中的表单页面样式如下图:
在这里插入图片描述其中海报的文件栏可以上传多张图片,属于但文本框选择多文件
源代码add.php如下:

<?php 
	//定义全局变量error_message
	$GLOBALS['error_message'] = '';
	function add_music(){
		//先设置一个data数组来存放数据
		$data = array(); 
		//data中的id值为随机生成
		$data['id'] = uniqid();
		//判断$_POST传过来的数据是否为空
		if(empty($_POST['title'])){
			$GLOBALS['error_message'] = '一定要输入歌名的哟';
			return;
		}
		if(empty($_POST['artist'])){
			$GLOBALS['error_message'] = '一定要输入歌手的哟';
			return;
		}
		//如果不为空,则存放到data数组中
		$data['title'] = $_POST['title'];
		$data['artist'] = $_POST['artist'];
		
		//检验客户端上传的图片文件
		if(!isset($_FILES['images'])){
			echo $_FILES['images'];
			$GLOBALS['error_message'] = '一定要输入图片文件的哟';
			return;
		}
		$images = $_FILES['images'];//'images'为表单中的name属性值
		$data['images'] = array();
		//var_dump($images['name']);
		for($i = 0; $i < count($images['name']); $i++){
			if($images['error'][$i] !== UPLOAD_ERR_OK){
				$GLOBALS['error_message'] = '请正确输入图片文件哟';
				return;
			}
			//校验文件大小
			if($images['size'][$i]>10*1024*1024){
				$GLOBALS['error_message'] = '图片文件过大';
				return;
			}
			if($images['size'][$i] < 1024){
				$GLOBALS['error_message'] = '图片文件过小';
				return;
			}
			//校验文件类型
			$allow_images = array('image/png','image/gif','image/jpeg');
			if(!in_array($images['type'][$i],$allow_images)){
				$GLOBALS['error_message'] = '不支持该图片文件类型';
				return;
			}
			$tarimg = '../unload/' . uniqid() . '-' . $images['name'][$i];
			if(!move_uploaded_file($images['tmp_name'][$i], $tarimg)){
				$GLOBALS['error_message'] = '上传失败图片文件哟';
				return;
			}
			$data['images'][] = substr($tarimg,2);
		}
		// var_dump($data);

		
		//检验客户端上传的音乐文件
		if(!isset($_FILES['source'])){
			echo $_FILES['source'];
			$GLOBALS['error_message'] = '一定要输入音乐文件的哟';
			return;
		}
		$source = $_FILES['source'];
		if($source['error'] !== UPLOAD_ERR_OK){
			$GLOBALS['error_message'] = '请正确输入音乐文件哟';
			return;
		}
		//校验文件大小
		if($source['size']>10*1024*1024){
			$GLOBALS['error_message'] = '音乐文件过大';
			return;
		}
		if($source['size']<1*1024*1024){
			$GLOBALS['error_message'] = '音乐文件过小';
			return;
		}
		//校验文件类型
		$allow_sources = array('audio/mp3','audio/wma','audio/mpeg');
		//var_dump($source['type']);
		if(!in_array($source['type'],$allow_sources)){
			$GLOBALS['error_message'] = '不支持该音乐文件类型';
			return;
		}
		
		//尽量用绝对路径,使用相对路径文件移动后容易出错
		$taradu = '../unload/' . uniqid() . '-' . $source['name'];
		if(!move_uploaded_file($source['tmp_name'],$taradu)){
			$GLOBALS['error_message'] = '上传失败音乐文件哟';
			return;
		}
		$data['source'] = substr($taradu,2);
		//echo "ok";
		
		//文件都上传成功后,要把数据写入json中
		//读取data.json文件内容
		$json = file_get_contents('data.json');
		$old = json_decode($json, true);
		//把data数组追加到旧的数据中
		array_push($old, $data);
		//把整合后的数据重新编码为json格式的字符串
		$new_json = json_encode($old);
		//把json格式的字符串写入文件
		file_put_contents('data.json', $new_json);
		//跳转回到list.php主页面
		header('Location: list.php');
	}
	if($_SERVER['REQUEST_METHOD'] === 'POST'){
		add_music();
	}
?>

<!DOCTYPE html>
<html lang="zh">
<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="../css/bootstrap.css">
</head>
<body>
	<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" class="container" enctype="multipart/form-data" autocomplete="off">
		<h2>添加歌曲</h2>
		<hr />
		<div class="form-group bg-danger">
			<?php if($error_message != ''){ ?>
					<h><?php echo $GLOBALS['error_message'] ?></h>
			<?php }?>
		</div>
		<div class="form-group">
			<label for="title">请输入歌名</label>
			<input type="text" name="title" class="form-control" id = "title">
		</div>
		<div class="form-group">
			<label for="artist">请输入歌手</label>
			<input type="text" name="artist" class="form-control" id = "artist">
		</div>
		<div class="form-group">
			<label for="images">请上传海报</label>
			<input type="file" name="images[]" class="form-control" id = "images" multiple="multiple" accept="image/*">
		</div>
		<div class="form-group">
			<label for="source">请上传音乐</label>
			<input type="file" name="source" class="form-control" id = "source" accept="audio/*">
		</div>
		<button type="submit" class="btn btn-default">Submit</button>
	</form>	
</body>
</html>

第三步:删除操作(delete.php)
主要的思路为:在list.php主页面点击按钮时,通过url地址中的 ?传递数据,核心代码为:<a class="btn btn-danger" href="delete.php?id=<?php echo $item['id']; ?>">删除</a>。然后在delete.php页面通过$_GET(‘id’)来接收。
源代码delete.php如下:

<?php 
	if(empty($_GET['id'])){
		exit('<h1>缺少必须的参数</h1>');
	}
	$id = $_GET['id'];
	$data = json_decode(file_get_contents('data.json'),true);
	foreach($data as $item){
		if($item['id'] !== $id) continue;
		$index = array_search($item,$data);
		array_splice($data,$index,1);
		$json = json_encode($data);
		file_put_contents('data.json',$json);
	}
	header('Location: list.php');
?>

提醒:编码过程中遇到的小问题有,当读取物理路径的文件时,浏览器可能会有一个不能加载本地资源的问题,解决办法参考:https://blog.51cto.com/lindianli/1557510

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值