- <input type="file" class="form-control-file w-100" accept="image/*" id="review_image" name="review_image" multiple="multiple">
复制代码- let formData = new FormData();
- $.each($('#review_image')[0].files, function (i, file) {
- formData.set('review_image'+ i, file);
- });
复制代码- $.ajax({
- url: '',
- type: 'post',
- data: formData,
- dataType: 'json',
- processData: false,
- contentType: false,
- cache: false,
- complete: function () {
- _this.removeAttr('disabled').html('Submit Review');
- },
- success: function (json) {
- _this.removeAttr('disabled').html('Submit Review')
-
- },
- error: function (xhr, ajaxOptions, thrownError) {
- _this.removeAttr('disabled').html('Submit Review');
-
- }
- });
复制代码
php
- $allowed = array(
- 'image/jpeg',
- 'image/pjpeg',
- 'image/png',
- 'image/x-png',
- 'image/gif'
- );
- $arr = [];
- $files = $this->request->files;
- if(count($files)>5){
- $json['error'] = 'image number limit 5';
- }
- foreach ($files as $file){
- if (!in_array($file['type'], $allowed)) {
- $json['error'] = 'image type error';
- }else{
- if($file['size']/1024/1024 > 5){
- $json['error'] = 'image size limit 5M';
- }else{
- $arr[] = ['tmp_name'=>$file['tmp_name'],'filetype'=>strtolower(strstr($file['name'],"."))];
- }
- }
- }
复制代码- $arr = [];
- foreach ($files as $file){
- $path = 'review';
- $path_sql = $path.'/'.date('Ym').'/'.date('d').'/'.date('Hi');
- $path = DIR_UPLOAD .$path_sql;
- if (!is_dir($path)) {
- @mkdir($path, 0777,true);
- }
- $filename = time().mt_rand(1000,9999).$file['filetype'];
- $path_sql = $path_sql.'/'.$filename;
- move_uploaded_file($file['tmp_name'], $path.'/'.$filename);
- $arr[]= "(".$review_id.",'".$path_sql."')";
- }
复制代码
|