php实现表单验证,表单文件验证,表单多文件上传,表单内容的增加和删除

list.php

<?php 
//获取那个json文件
$contents=file_get_contents('storage.json');
//将这个文件里面的内容转换成json数据格式。。。。。。。数组
//第二个参数是将它转换成json数组的形式,默认值为false,如果是false,则转换的是对象的形式
$arr=json_decode($contents,true);


 ?>


<!doctype html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>音乐列表</title>
     <link rel="stylesheet" href="bootstrap.css">
 </head>
 <body>
 <div class="container my-5">    
    <h1 class="display-2 mb-5">音乐列表<a href="add.php" class="btn btn-primary">添加</a></h1>
    <hr>
    <table class="table table-bordered table-striped table-hover">
    <thead class="thead-inverse">
        <tr style="background-color:black;color:#fff">
                <th>编号</th>
                <th>标题</th>
                <th>歌手</th>
                <th>海报</th>
                <th>音乐</th>
                <th>操作</th>
        </tr>
    </thead>
    <tbody class="text-center">
        <?php foreach ($arr as $item): ?>
           <tr>
               <td class="align-middle"><?php echo $item['id']; ?></td>
               <td class="align-middle"><?php echo $item['title']; ?></td>
               <td class="align-middle"><?php echo $item['artist']; ?></td>
               <td class="align-middle"><img src="<?php echo $item['images'][0]; ?>"></td>
               <td class="align-middle"> <audio src="<?php echo $item['source']; ?>"></audio></td>
               <td class="align-middle"><button class="btn btn-danger btn-sm">删除</button></td>
           </tr> 

        <?php endforeach ?>

        
    </tbody> 
    </table>
 </div>
 </body>
 </html>

 add.php

<?php

function add () {
  // 目标:接收客户端提交的数据和文件,最终保存到数据文件中
  $data = array(); // 准备一个空的容器,用来装最终要保存的 数据
  

  // 1. 接收提交的文本内容
  // ===================================================
  if (empty($_POST['title'])) {
    $GLOBALS['error_message'] = '请输入音乐标题';
    return;
  }
  if (empty($_POST['artist'])) {
    $GLOBALS['error_message'] = '请输入歌手名称';
    return;
  }

  // 2. 接收图片文件
  // =======================================================
  // 如何接收单个文件域的多文件上传???
  if (empty($_FILES['images'])) {
    $GLOBALS['error_message'] = '请正常使用表单';
    return;
  }

  $images = $_FILES['images'];
  // 准备一个容器装所有的海报路径
  $data['images'] = array();

  // 遍历这个文件域中的每一个文件(判断是否成功、判断类型、判断大小、移动到网站目录中)
  for ($i = 0; $i < count($images['name']); $i++) {
    // $images['error'] => [0, 0, 0]
    if ($images['error'][$i] !== UPLOAD_ERR_OK) {
      $GLOBALS['error_message'] = '上传海报文件失败1';
      return;
    }

    // 类型的校验
    // $images['type'] => ['image/png', 'image/jpg', 'image/gif']
    if (strpos($images['type'][$i], 'image/') !== 0) {
      $GLOBALS['error_message'] = '上传海报文件格式错误';
      return;
    }

    // TODO: 文件大小的判断
    if ($images['size'][$i] > 1 * 1024 * 1024) {
      $GLOBALS['error_message'] = '上传海报文件过大';
      return;
    }

    // 移动文件到网站范围之内
    $dest = './uploads/' . uniqid() . $images['name'][$i];
    if (!move_uploaded_file($images['tmp_name'][$i], $dest)) {
      $GLOBALS['error_message'] = '上传海报文件失败2';
      return;
    }
    //从 $dest 第一个字符开始取
    $data['images'][] = substr($dest, 2);
  }

   //判断用户有没有文件表单域
  if (empty($_FILES['source'])) {
    $GLOBALS['error_message'] = '请正常使用表单';
    return;
  }

  $source = $_FILES['source'];
  // => { name: , tmp_name .... }

  // 判断是否上传成功
  if ($source['error'] !== UPLOAD_ERR_OK) {
    $GLOBALS['error_message'] = '上传音乐文件失败1';
    return;
  }
  // 判断类型是否允许
  $source_allowed_types = array('audio/mp3', 'audio/wma');
  if (!in_array($source['type'], $source_allowed_types)) {
    $GLOBALS['error_message'] = '上传音乐文件类型错误';
    return;
  }
  // 判断大小
  if ($source['size'] < 1 * 1024 * 1024) {
    $GLOBALS['error_message'] = '上传音乐文件过小';
    return;
  }
  if ($source['size'] > 10 * 1024 * 1024) {
    $GLOBALS['error_message'] = '上传音乐文件过大';
    return;
  }
  // 移动
  $target = './uploads/' . uniqid() . '-' . $source['name'];
  if (!move_uploaded_file($source['tmp_name'], $target)) {
    $GLOBALS['error_message'] = '上传音乐文件失败2';
    return;
  }
  // 将数据装起来
  // 保存数据的路径一定使用绝对路径存
  $data['id'] = uniqid();
  // 记下 title 和 artist
  $data['title'] = $_POST['title'];
  $data['artist'] = $_POST['artist'];
  $data['source'] = substr($target, 2);

  // 将数据加入到原有数据中
  // 因为要往里面添加数组,所以要先把它拿出来
  $json = file_get_contents('storage.json');
  $old = json_decode($json, true);
  //添加数组
  array_push($old, $data);
  //变成json格式的数据
  $new_json = json_encode($old);
  //往storage.json里面放
  file_put_contents('storage.json', $new_json);

  // 都完成后页面跳转
  header('Location: list.php');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  add();
}

?>
<!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($error_message)): ?>
    <div class="alert alert-danger">
      <?php echo $error_message; ?>
    </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>
      </div>
      <div class="form-group">
        <label for="source">音乐</label>
        <!-- accept 可以设置两种值分别为  MIME Type / 文件扩展名 -->
        <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>

delete.php

<?php 
if(empty($_GET['id'])){
    //没在函数里面尽量不用return,用了的话会影响后面代码的执行
    exit('<h1>必须指定参数</h1>');
}
$id=$_GET['id'];
$json=file_get_contents('storage.json');
$data=json_decode($json,true);
foreach ($data as $item) {
    if($item['id']!==$id){
        continue;
    }
    $index=array_search($data,$item);
    array_splice($data, $index,1);
}
$json=json_encode($data);
file_put_contents('storage.json', $json);
header('Location:list.php');





 ?>

storage.json

[{"images":["uploads\/5e8dbca50d20059f2aea381a25icon-03.png","uploads\/5e8dbca51668d59f2aea3810cficon-02.png","uploads\/5e8dbca51763459f2d87f43511icon-02.png"],"id":"5e8dbca51b638","title":"sdsd","artist":"sdsd","source":"uploads\/5e8dbca519136-a_hisa - \u307b\u305f\u308b\u706b (\u8424\u706b\u866b\u4e4b\u5149).mp3"}]

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值