Detailed explanation of the steps to upload multiple images using Bootstrap+PHP

PHPz
Release: 2023-03-26 08:22:02
Original
2868 people have browsed it

This time I will bring you a detailed explanation of the steps to implement multiple image uploads in Bootstrap PHP. What are the precautions for implementing multiple image uploads in Bootstrap PHP? The following is a practical case, let’s take a look at it [Related video recommendations: Bootstrap tutorial

The interface of using bootstrap is beautiful, can be previewed, can be dragged and uploaded, and can be uploaded asynchronously or synchronously with ajax. The following is the rendering:

Front-end code: fileinput.html

<!DOCTYPE html>
<!-- release v4.1.8, copyright 2014 - 2015 Kartik Visweswaran -->
<html lang="en">
 <head>
  <meta charset="UTF-8"/>
  <title>bootstrap多图上传</title>
  <link href="/public/index/fileinput/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet">
  <link href="/public/index/fileinput/css/fileinput.css" rel="external nofollow" media="all" rel="stylesheet" type="text/css" />
  <script src="/public/index/fileinput/js/jquery-2.0.3.min.js"></script>
  <script src="/public/index/fileinput/js/fileinput.js" type="text/javascript"></script>
  <script src="/public/index/fileinput/js/bootstrap.min.js" type="text/javascript"></script>
  <!-- 中文化 -->
  <script src="/public/index/fileinput/js/fileinput_locale_zh.js" type="text/javascript"></script>
 </head>
 <body>
  <p class="container kv-main">   
   <br>
   <form enctype="multipart/form-data">    
    <p class="form-group">
     <!-- 初始化插件 -->
     <input id="file-1" type="file" multiple class="file" data-overwrite-initial="false" data-min-file-count="2" name="images">
    </p>
    
   </form>
  </p>
 </body>
 <script>
 // 初始化filleinput控件 第一次初始化
 function initFileInput(ctrlName, uploadUrl){
  var control = $('#'+ctrlName);
  control.fileinput({
   language: 'zh', //设置语言
   uploadUrl:uploadUrl, //上传的地址
   allowedFileExtensions:['jpg','png'], //接收的文件后缀
   showUpload:true, //是否显示上传按钮
   showCaption:false, //是否显示标题
   maxFileSize: 1000, //图片最大尺寸kb 为0不限制
   maxFilesNum: 3,  //最多上传图片
   overwriteInitial: false,//不覆盖已上传的图片
   browseClass: "btn btn-info", //按钮样式 
   dropZoneEnabled: true,//是否显示拖拽区域
   previewFileIcon: "<i class=&#39;glyphicon glyphicon-king&#39;></i>",
   msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
  });
 }
 //初始化fileinput控件,第一次初始化 (控件id,上传地址)
 initFileInput("file-1", "uploadImg");
 // 监听事件
 $("#file-1").on("fileuploaded", function (event, data, previewId, index) {
  // 上传地址
  console.log(data);
 });
 </script>
</html>
Copy after login

Back-end code:

 /*
 * bootst多图上传
 */
 public function fileinput()
 {
  return $this->fetch();
 }
 public function uploadImg()
 {
  // var_dump($_FILES);
  // 获取表单上传文件 
  $file = request()->file(&#39;images&#39;);
  // 移动到框架应用根目录/public/uploads/img 目录下
  $info = $file->move(ROOT_PATH . &#39;public&#39; . DS . &#39;uploads/img&#39;);
  if($info){
   // 成功上传后 获取上传信息
   $data[&#39;response&#39;] = $info->getSaveName();
   return json($data);
   //图片上传成功,以下可对数据库操作
   // ......
  }else{
   // 上传失败获取错误信息
   echo $file->getError();
  }
 }
Copy after login

I believe you have mastered the method after reading the case in this article, and more How exciting, please pay attention to other related articles on php Chinese website!

Recommended reading:

Detailed explanation of steps to prevent repeated form submission in PHP Session

PHP caching tool XCache installation and use case analysis

The above is the detailed content of Detailed explanation of the steps to upload multiple images using Bootstrap+PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template