This article through the example code to introduce to you based on the PHP implementation of multi-image upload and single-image upload function, the code is easy to understand, very good, with a certain reference value, the need for friends can refer to the next
The following section of code to introduce PHP implementation of single-image upload and multi-image upload function, the specific code is as follows:
/** * Multi-select picture upload * * @version v1.0.0 * @author * @since 17-11-24 */Public Function upload () {$file = $_f iles[' file '; Empty ($file) && $this->response (201, ' Please select the file to upload '); unset ($_files[' file '); $count = count ($file [' name ']); Number of uploaded images $count > && $this->response (203, ' bulk upload images upload up to 10 images at a time '); $tmpFile = []; $returnData = []; for ($i =0; $i < $count; $i + +)//Cycle picture {$tmpFile [' name '] = $file [' name '] [$i]; $tmpFile [' type '] = $file [' type '] [$i]; $tmpFile [' tmp_name '] = $file [' Tmp_name '] [$i]; $tmpFile [' error '] = $file [' ERROR '] [$i]; $tmpFile [' size '] = $file [' Size '] [$i]; $_files[' File_ '. $i] = $tmpFile; Determines whether the allowed picture type $ext = substr ($_files[' file_ '. $i [' Name '],strrpos ($_files[' file_ '. $i [' name '], '. ') +1); Upload file suffix stripos (' jpeg|png|bmp|jpg ', $ext) = = = FALSE && $this->response (210, ' image format support JPEG, PNG, BMP format Picture '); $data = $this->uploadone (' File_ '. $i, ' jpeg|pNg|bmp|jpg '); if ($data [' status '] = = 1) {$this->response (500, ' No. '). $i + 1). ' Image upload failed, '. $data [' msg ']); } $returnData [$i] [' url '] = $data [' url ']; The picture path $returnData [$i] [' old_name '] = substr ($tmpFile [' Name '],0,strrpos ($tmpFile [' name '], '. ')); The original name of the picture} $this->response ("successful", $returnData); */** * Single File Upload * @version v1.0.0 * @author * @since 17-11-24 * @param $file Upload Form name * @param $type Upload type * @param $maxSize upload file limit size (default 10M) */Private Function uploadone ($filename = ' file ', $type = ' jpeg|png|bmp|jpg ', $maxSize = 10240) {list ($width, $height) = getimagesize ($_files[$filename [' tmp_name ']);//Get the picture's width and height list ($use C, $sec) = Explode ("", Microtime ()); $time = $sec. substr ($usec, 2); Number of seconds + microseconds $ext = substr ($_files[$filename [' Name '],strrpos ($_files[$filename] [' name '], '. ') +1); Upload file suffix $name = $time. '-'. $width. ' * '. $height. '. $ext; $filePath = $_files[$filename [' tMp_name ']; $type = $_files[$filename [' type ']; $this->load->library (' Qiniu '); $returnData [' url '] = $this->qiniu->upload ($name, $filePath, $type); $returnData [' status '] = 0; return $returnData; }