PHP realizes multi graph uploading and single graph uploading functions
- 2021-10-13 06:46:57
- OfStack
The following 1 code introduces the function of php to realize single graph upload and multi-graph upload. The specific code is as follows:
/**
* Multiple Picture Upload
*
* @version v1.0.0
* @author
* @since 17-11-24
*/
public function upload()
{
$file = $_FILES['file'];
empty($file) && $this->response(201,' Please select a file to upload ');
unset($_FILES['file']);
$count = count($file['name']); // Number of pictures uploaded
$count > 10 && $this->response(203,' Upload pictures in batches 1 Second most upload 10 A picture ');
$tmpFile = [];
$returnData = [];
for($i=0;$i<$count;$i++) // Loop through pictures
{
$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;
// It is determined whether it is an 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. 1 '.($i+1).' A picture failed to upload, '.$data['msg']);
}
$returnData[$i]['url'] = $data['url']; // Picture path
$returnData[$i]['old_name'] = substr($tmpFile['name'],0,strrpos($tmpFile['name'], '.')); // Original name of picture
}
$this->response(200,'successful',$returnData);
}
/**
* Single file upload
* @version v1.0.0
* @author
* @since 17-11-24
* @param $file Upload a form name Name
* @param $type Upload type
* @param $maxSize Upload file size limit ( Default 10M)
*/
private function uploadOne($filename = 'file',$type = 'jpeg|png|bmp|jpg',$maxSize = 10240)
{
list($width,$height) = getimagesize($_FILES[$filename]['tmp_name']); // Gets the width and height of the picture
list($usec, $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;
}
Summarize