<?php /** * Created by PhpStorm. * User: DY040 * Date: 2018/4/26 * Time: 13:23 */ echo '<pre>'; /* * Support multiple file uploads to check whether all files meet the requirements and store them on the server and return the image address * */ class ImgUpload1 { protected static $imgs_file = array (); // Data to be returned (stored file information) function __construct($files) { foreach ($files as $k => $v) { $img_info = $files[$k]; $arr = array(); if (gettype($img_info['name']) === 'array') { foreach ($img_info['name'] as $key => $value) { $arr['name'] = $img_info['name'][$key]; $arr['type'] = $img_info['type'][$key]; $arr['tmp_name'] = $img_info['tmp_name'][$key]; $arr['error'] = $img_info['error'][$key]; $arr['size'] = $img_info['size'][$key]; $this->_check($arr, $k); } } else { $this->_check($img_info, $k); } } } protected function _check($img_info, $index) { if (!isset(static::$imgs_file[$index])) { static::$imgs_file[$index] = $this->_img_store($img_info) . "*"; } else { static::$imgs_file[$index] .= $this->_img_store($img_info) . "*"; } } protected function _img_store($img_info) { // Check if the file is legal---File name suffix $img_hzm = array ('.gif', '.png', '.jpg', '.jpeg' ); $re = in_array ( strrchr ( $img_info ['name '], '.'), $img_hzm ); if (! $re ) { return 'This image is invalid - file name suffix' ; } // Check whether the file is legal---MIME php detects the mime of uploaded information if ( explode ('/', $img_info ['type'])[0] !== 'image' ) { return 'The image is invalid- MIME' ; } // Check whether the file is legal---MIME php detects local temporary files // Here we need to modify php.ini to add php_fileinfo.dll module $file_info = new Finfo(FILEINFO_MIME_TYPE); $mime_type = $file_info -> file ( $img_info ['tmp_name' ]); if ( explode ('/', $mime_type )[0] !== 'image' ) { return 'This image is not valid - MIME' ; } // Set the file size to 1mb if ( $img_info ['size'] > 1000000 ) { return 'The image is invalid - the file is larger than 1000kb' ; } // Is it a temporary file uploaded by http if (! is_uploaded_file ( $img_info ['tmp_name' ])) { echo 'The file name is invalid' ; return false ; }; // Set the upload file address if (! is_dir ('.\upload' )) { mkdir ('.\upload' ); } $path = dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . 'upload\\' . date ('Ymdh'); // Divide directory by hour $imgType = strrchr ( $img_info ['name'], '.'); // File name suffix $re = is_dir($path); if (!$re) { mkdir($path); } $imgName = uniqid(rand(), true) . $imgType; $img_file = $path . DIRECTORY_SEPARATOR . $imgName; $upload_re = move_uploaded_file($img_info['tmp_name'], $img_file); if ($upload_re) { return $img_file; } else { return 'File upload failed' ; } } public function imgs_file() { $arr = static::$imgs_file; foreach ($arr as $k => $v) { if(gettype($_FILES[$k]['name'])==='string'){ $arr[$k] = substr($v, 0, -1); }else{ $arr[$k] = explode('*', substr($v, 0, -1)); } } return $arr; } } $img_upload = new ImgUpload1( $_FILES ); // Get the file location print_r ( $img_upload -> imgs_file()); /* * * Array ( [user_img] => D:\PHP\www\hxq\day22\upload\2018042604\183775ae1878615f2e6.81933117.jpg [pro_img] => Array ( [0] => D:\PHP\www\hxq\day22\upload\2018042604\324625ae18786172b65.94418381.jpg [1] => D:\PHP\www\hxq\day22\upload\2018042604\291675ae187861863e5.14296700.jpg ) ) * * */ /* * Corresponding form * * <form action="imgUpload.class.php" method="post" enctype="multipart/form-data"> <p><input type="file" name="user_img"></p> <p><input type="file" name="pro_img[]"></p> <p><input type="file" name="pro_img[]"></p> <p><input type="submit"></p> </form> * */