PHP音乐列表

php音乐列表

  • add.php
<?php

// 表单三部曲
// 1. 接收校验
// 2. 持久化
// 3. 响应

function add_music()
{
    $data = array(); // 准备一个空的数组,用来存储通过验证的数据
    $data['id'] = uniqid();
    // 1. 接收校验
    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'];

    // 判断上传是否成功  UPLOAD_ERR_OK == 0
    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;
    }

    // 音乐上传和移动都ok
    $data['source'] = $target1; // 保存音乐

    // ======== 图片的上传和移动 ========

    if (!isset($_FILES['images'])) {
        $GLOBALS['msg'] = '哎呀别闹';
        return;
    }

    // var_dump($_FILES['images']); => 确保数据准确

    // 接收到了图片文件
    $images = $_FILES['images'];
    // 创建一个空数组准备存储多张图片
    $data['images'] = array();
    // for循环 ==> i变量
    // count ==> 计算数组的长度
    for ($i = 0; $i < count($images['error']); $i++) {
        // 图片的error做校验
        // $images['error'] ==> [0, 0, 0]
        if ($images['error'][$i] !== UPLOAD_ERR_OK) {
            $GLOBALS['msg'] = '图片上传失败1';
            return; // 应该全部上传失败还是跳过失败的那一张图片取决于你的业务
        }
        // 大小校验 ==> 自己写

        // 图片类型的校验
        // $images['type'] ==> 'image/jpg','image/png','image/jpeg'
        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;
        }
        // $images_all = $data['images'];
        // $images_all[] = $target2;

        $data['images'][] = $target2;
    }

    // 保存数据 ==> 写入到json文件
    // json字符串转成数组
    $arr = json_decode(file_get_contents('music.json'), true);
    array_push($arr, $data); // 往数组中添加数据
    // var_dump($arr); => 确保添加后的数据没有问题

    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>
  • delete.php
<?php

// 判断用户需要删除的是哪一条数据? 通过id来判断

// 获取url中的 id

// 判断 id 为空的情况
if(empty($_GET['id'])){
    // return; ==> 不在函数内部,不能return
    exit('<h1>哎呀别闹</h1>');
}

// 接收id
$id = $_GET['id'];

// 读取music.json文件
$data = json_decode(file_get_contents('music.json'),true);

// 遍历 $data 数组
foreach ($data as $key) {
    // 不是我们要找的数据 ==> 跳过
    if($key['id'] !== $id) continue;
    // 查找数据所在的位置  $key -> 等于用户要删除的id
    $index = array_search($key, $data);
    // 删除这条数据
    array_splice($data, $index, 1);
    // var_dump($data); => 确认删除后的数据无误

    // 把最新的数据写入到music.json文件
    file_put_contents('music.json',json_encode($data));
}

// 跳转到list.php页面
header('Location:list.php');

; ?>
  • list.php
<?php

// TODO: 读取music.json文件, 动态渲染到页面

$contents = file_get_contents('music.json'); // 字符串

// 把对象转成json字符串的过程叫做序列化
// 把json字符串转成对象的过程叫做反序列化

// json 字符串 ==> 对象 ==> stdClass 对象
// 默认转成一个 stdClass 对象, 如果要转成数组, 添加一个参数 true
$arr = json_decode($contents, true);
    // $data = json_decode($contents); 对象, 取其成员 $data -> title

    // var_dump($arr); ==> 数组
; ?>

<!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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haoge568

不要超过你的早饭钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值