php怎么实现音乐列表

发布时间:2022-05-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php怎么实现音乐列表脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

phP实现音乐列表的方法:1、从文件当中读取内容,并解码;2、将数据展示在列表,并利用foreach对数据一一显示在列表当中即可。

php怎么实现音乐列表

本文操作环境:Windows7系统、PHP7.1版、DELL G3

php怎么实现音乐列表?

PHP实现音乐列表的上传、展示、删除:

思路
list.php列表展示
1、从文件当中读取内容,并解码

$JSON = file_get_contents('data.json');$songs = json_decode($json, true);

2、将数据展示在列表,利用foreach对数据一一显示在列表当中

<?php foreach ($songs as $ITem): ?>
        <tr>
          <td class="align-middle"><?php echo $item['title']; ?></td>
        </tr><?php enDForeach ?>

add.php
1、表单提交给自身网页进行处理
2、给每个输入框一个name值
3、php处理的时候,可以通过$_POST[‘title’]对数据进行处理
4、处理之后将数据保存到文件/数据库当中

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctyPE=";multipart/form-data"><label for="title">标题</label>
        <input type="text" class="form-control " id="title" name="title"> <button class="BTn btn-Primary btn-block">保存</button></form>
// 读取已有数据
  $songs = json_decode(file_get_contents('data.json'), true);
  // 追加新数据
  $songs[] = $new_song;
  // 将追加的结果写入文件
  file_put_contents('data.json', json_encode($songs));

del.php
1、需要执行删除就必须提供你想要删除的是谁,获取要删除的ID
这里的话 是通过一开始从文件/数据库当中读取出来的$item[‘id’]值,就可以判断是要删除哪一个,同时删除按钮a标签?后面是可以附带值传输给后台的

 <a class="btn btn-outline-danger btn-sm" href="del.php?id=<?php echo $item['id']; ?>">删除</a>

2、从数据库/文件当中读取数据
3、通过ID在数据中找到对应要删除的键,删除之后将数据放回文件/数据库中


del.php

<?php// 只要有人请求我 del.php 我就执行删除操作// 如果需要我执行删除就必须提供你想要删除的是谁// 一般情况下如果客户端需要给服务端提供简单的数据标识,// 这种情况都会采用URL 地址传递问号参数的方式传递// 校验(客户端来的东西都不能信)if (empty($_GET['id'])) {
  exit('你必须提供要删除的数据ID'); // exit 会直接结束脚本的运行}// 确保客户端提交了 ID$id = $_GET['id'];// 1. 读取已有数据$json = file_get_contents('data.json');// 2. 反序列化$songs = json_decode($json, true);// 3. 遍历数组找到要删除的元素foreach ($songs as $item) {
  if ($item['id'] === $id) {
    // 找到了要删除的数据
    // 4. 在数组中删除这个元素
    // 4.1. 找到这个数据在数组的下标
    $index = array_seArch($item, $songs);
    array_splice($songs, $index, 1);
    // 5. 将删除过后的数组序列化成 JSON 字符串
    $new_json = json_encode($songs);
    // 6. 持久化
    file_put_contents('data.json', $new_json);
    break;
  }}// 跳转回去header('Location: /songs/list.php');

add.php

<?php

function receive_form () {
  // global $error_type;
  // 1. 校验客户端提交的数据
  // 1.1. 校验标题
  // empty($_POST['title']) === !(isset($_POST['title']) && $_POST['title'] !== '')
  // empty函数的作用就是判断一个成员是否为空(未定义、值为false)
  if (empty($_POST['title'])) {
    // 标题未正常填写
    $GLOBALS['error_type'] = 'title';
    $GLOBALS['error_msg'] = "填写标题";
    return;
  }

  if (empty($_POST['artist'])) {
    // 歌手未正常填写
    $GLOBALS['error_type'] = 'artist';
    $GLOBALS['error_msg'] = "填写歌手";
    return;
  }

  // ===================================================

  // echo "校验文件";
  // 校验上传文件
  //  1. 校验是否上传成功(error)
  if ($_FILES['source']['error'] !== UPLOAD_ERR_OK) {
    $GLOBALS['error_type'] = 'source';
    $GLOBALS['error_msg'] = "上传失败";
    return;
  }

  //  2. 校验上传文件的类型(type)
  $Allowed_source_types = array('audio/mP3', 'audio/wma');
  if (!in_array($_FILES['source']['type'], $allowed_source_types)) {
    $GLOBALS['error_type'] = 'source';
    $GLOBALS['error_msg'] = "只能上传音频文件";
    return;
  }

  //  3. 校验文件大小(size)文件的大小单位是字节
  if (1 * 1024 * 1024 > $_FILES['source']['size'] || $_FILES['source']['size'] > 10 * 1024 * 1024) {
    $GLOBALS['error_type'] = 'source';
    $GLOBALS['error_msg'] = "上传文件大小不合理";
    return;
  }

  //  将文件从临时目录中移动到网站下面
  $tmp_path = $_FILES['source']['tmp_name']; // 临时路径
  $dest_path = '../uploads/mp3/' . $_FILES['source']['name']; // 存放路径
  $source = substr($dest_path, 2);
  $moved = move_uploaded_file($tmp_path, $dest_path); // 返回移动是否成功

  if (!$moved) {
    $GLOBALS['error_type'] = 'source';
    $GLOBALS['error_msg'] = "上传失败";
    return;
  }

  // ============= 处理多个文件逻辑 ====================

  // 如果一个文件域是多文件上传的话,文件域的 name 应该是由 [] 结尾
  for ($i = 0; $i < count($_FILES['images']['error']); $i++) {
    // 1. 校验上传成功
    if ($_FILES['images']['error'][$i] !== UPLOAD_ERR_OK) {
      $GLOBALS['error_type'] = 'images';
      $GLOBALS['error_msg'] = "上传图片失败";
      return;
    }
    // 2. 校验文件类型
    $allowed_images_types = array('image/jpeg', 'image/png', 'image/gif');
    if (!in_array($_FILES['images']['type'][$i], $allowed_images_types)) {
      $GLOBALS['error_type'] = 'images';
      $GLOBALS['error_msg'] = "只能上传图片文件";
      return;
    }
    // 3. 校验大小
    if ($_FILES['images']['size'][$i] > 1 * 1024 * 1024) {
      $GLOBALS['error_type'] = 'images';
      $GLOBALS['error_msg'] = "上传文件大小不合理";
      return;
    }
    // 移动文件
    $img_tmp_path = $_FILES['images']['tmp_name'][$i]; // 临时路径
    $img_dest_path = '../uploads/img/' . $_FILES['images']['name'][$i]; // 存放路径
    $img_moved = move_uploaded_file($img_tmp_path, $img_dest_path); // 返回移动是否成功
    if (!$img_moved) {
      $GLOBALS['error_type'] = 'images';
      $GLOBALS['error_msg'] = "上传图片失败";
      return;
    }

    $images[] = substr($img_dest_path, 2);
  }

  // 2. 保存数据
  $new_song = array(
    'id' => uniqid(), // uniqid 获取一个唯一ID
    'title' => $_POST['title'],
    'artist' => $_POST['artist'],
    'images' => $images,
    'source' => $source
  );
  // 读取已有数据
  $songs = json_decode(file_get_contents('data.json'), true);
  // 追加新数据
  $songs[] = $new_song;
  // 将追加的结果写入文件
  file_put_contents('data.json', json_encode($songs));

  // 3. 响应
  header('Location: /songs/list.php');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // 处理接收校验表单
  receive_form();
}

?>
<!DOCTYPE htML>
<html>
<head>
  <meta charset="UTF-8">
  <title>添加新音乐</title>
  <link rel="stylesheet" href="bootstrap.css">
</head>
<body>
  <div class="container py-5">
    <h1>添加新音乐</h1>
    <hr>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
      <div>
        <label for="title">标题</label>
        <input type="text" class="form-control <?php echo isset($error_type) &amp;& $error_type === 'title' ? 'is-invalid' : ''; ?>" id="title" name="title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : ''; ?>">
        <small><?php echo $error_msg; ?></small>
      </div>
      <div>
        <label for="artist">歌手</label>
        <input type="text" class="form-control <?php echo isset($error_type) && $error_type === 'artist' ? 'is-invalid' : ''; ?>" id="artist" name="artist" value="<?php echo isset($_POST['artist']) ? $_POST['artist'] : ''; ?>">
        <small><?php echo $error_msg; ?></small>
      </div>
      <div>
        <label for="images">海报</label>
        <!-- multiple 可以让文件域多选 -->
        <!-- accept 可以指定文件域能够选择的默认文件类型 MIME Type -->
        <!-- image/* 代表所有类型图片 -->
        <!-- 除了使用 MIME 类型 还可以使用文件后缀名限制:.png,.jpg -->
        <input type="file" id="images" name="images[]" multiple accept="image/*">
      </div>
      <div>
        <label for="source">音乐</label>
        <input type="file" class="form-control <?php echo isset($error_type) && $error_type === 'source' ? 'is-invalid' : ''; ?>" id="source" name="source" accept="audio/*">
        <small><?php echo $error_msg; ?></small>
      </div>
      <button class="btn btn-PRimary btn-block">保存</button>
    </form>
  </div>
</body>
</html>

list.php

<?php

// 1. 读取文件内容
$json = file_get_contents('data.json');
// 2. 反序列化
// json_decode 第二个参数可以用来指定返回数据都采用 关联数组的方式 描述对象
$songs = json_decode($json, true);
// 3. 遍历数据渲染HTML
// var_dump($songs);

?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>音乐列表</title>
  <link rel="stylesheet" href="bootstrap.css">
</head>
<body>
  <div class="container py-5">
    <h1>音乐列表</h1>
    <hr>
    <div class="px-2 mb-3">
      <a href="add.php" class="btn btn-secondary btn-sm">添加</a>
    </div>
    <table class="table table-bordered table-striped table-hover">
      <thead>
        <tr>
          <th><input type="checkbox" name="" id=""></th>
          <th>标题</th>
          <th>歌手</th>
          <th>海报</th>
          <th>音乐</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($songs as $item): ?>
        <tr>
          <td><input type="checkbox" name="" id=""></td>
          <td><?php echo $item['title']; ?></td>
          <td><?php echo $item['artist']; ?></td>
          <td>
            <?php foreach ($item['images'] as $img): ?>
            <img src="<?php echo $img; ?>" alt="">
            <?php endforeach ?>
          </td>
          <td><audio src="<?php echo $item['source']; ?>" controls></audio></td>
          <td>
            <a class="btn btn-outline-danger btn-sm" href="del.php?id=<?php echo $item['id']; ?>">删除</a>
            <!-- hidden 隐藏域 -->
            <!-- <form action="del.php" method="get">
              <input type="hidden" name="id" value="<?php echo $item['id']; ?>">
              <button class="btn btn-danger btn-sm">删除</button>
            </form> -->
          </td>
        </tr>
        <?php endforeach ?>
      </tbody>
    </table>
  </div>
</body>
</html>

推荐学习:《PHP视频教程》

以上就是php怎么实现音乐列表的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的php怎么实现音乐列表全部内容,希望文章能够帮你解决php怎么实现音乐列表所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:php