多图上传

[复制链接]
admin 发表于 2021-10-8 13:06:58 | 显示全部楼层 |阅读模式 0
  1. <input type="file" class="form-control-file w-100" accept="image/*" id="review_image" name="review_image" multiple="multiple">
复制代码
  1. let formData = new FormData();
  2.                                 $.each($('#review_image')[0].files, function (i, file) {
  3.                                     formData.set('review_image'+ i, file);
  4.                                 });
复制代码
  1. $.ajax({
  2.     url: '',
  3.     type: 'post',
  4.     data: formData,
  5.     dataType: 'json',
  6.     processData: false,
  7.     contentType: false,
  8.     cache: false,
  9.     complete: function () {
  10.         _this.removeAttr('disabled').html('Submit Review');
  11.     },
  12.     success: function (json) {
  13.         _this.removeAttr('disabled').html('Submit Review')
  14.         
  15.     },
  16.     error: function (xhr, ajaxOptions, thrownError) {
  17.         _this.removeAttr('disabled').html('Submit Review');
  18.         
  19.     }
  20. });
复制代码


php
  1. $allowed = array(
  2.                 'image/jpeg',
  3.                 'image/pjpeg',
  4.                 'image/png',
  5.                 'image/x-png',
  6.                 'image/gif'
  7.             );
  8.             $arr = [];
  9.             $files = $this->request->files;
  10.             if(count($files)>5){
  11.                 $json['error'] = 'image number limit 5';
  12.             }
  13.             foreach ($files as $file){
  14.                 if (!in_array($file['type'], $allowed)) {
  15.                     $json['error'] = 'image type error';
  16.                 }else{
  17.                     if($file['size']/1024/1024 > 5){
  18.                         $json['error'] = 'image size limit 5M';
  19.                     }else{
  20.                         $arr[] = ['tmp_name'=>$file['tmp_name'],'filetype'=>strtolower(strstr($file['name'],"."))];
  21.                     }
  22.                 }
  23.             }
复制代码
  1. $arr = [];
  2.         foreach ($files as $file){
  3.             $path = 'review';
  4.             $path_sql = $path.'/'.date('Ym').'/'.date('d').'/'.date('Hi');
  5.             $path = DIR_UPLOAD .$path_sql;
  6.             if (!is_dir($path)) {
  7.                 @mkdir($path, 0777,true);
  8.             }
  9.             $filename = time().mt_rand(1000,9999).$file['filetype'];
  10.             $path_sql = $path_sql.'/'.$filename;
  11.             move_uploaded_file($file['tmp_name'], $path.'/'.$filename);
  12.             $arr[]= "(".$review_id.",'".$path_sql."')";
  13.         }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

公告上一条 /1 下一条