/**
* 多選圖片上傳
*
* @version v1.0.0
* @author
* @since 17-11-24
*/
public function upload()
{
$file = $_FILES['file'];
empty($file) $this->response(201,'請選擇要上傳的文件');
unset($_FILES['file']);
$count = count($file['name']); // 上傳圖片的數(shù)量
$count > 10 $this->response(203,'批量上傳圖片一次最多上傳10張圖片');
$tmpFile = [];
$returnData = [];
for($i=0;$i$count;$i++) // 循環(huán)處理圖片
{
$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;
// 判斷是否是允許的圖片類型
$ext = substr($_FILES['file_'.$i]['name'],strrpos($_FILES['file_'.$i]['name'],'.')+1); // 上傳文件后綴
stripos('jpeg|png|bmp|jpg',$ext) === FALSE $this->response(210,'圖片格式支持 JPEG、PNG、BMP格式圖片');
$data = $this->uploadOne('file_'.$i,'jpeg|png|bmp|jpg');
if($data['status'] == 1)
{
$this->response(500,'第'.($i+1).'張圖片上傳失敗,'.$data['msg']);
}
$returnData[$i]['url'] = $data['url']; // 圖片路徑
$returnData[$i]['old_name'] = substr($tmpFile['name'],0,strrpos($tmpFile['name'], '.')); // 圖片原名稱
}
$this->response(200,'successful',$returnData);
}
/**
* 單文件上傳
* @version v1.0.0
* @author
* @since 17-11-24
* @param $file 上傳表單name名稱
* @param $type 上傳類型
* @param $maxSize 上傳文件限制大小(默認 10M)
*/
private function uploadOne($filename = 'file',$type = 'jpeg|png|bmp|jpg',$maxSize = 10240)
{
list($width,$height) = getimagesize($_FILES[$filename]['tmp_name']); // 獲取圖片的寬和高
list($usec, $sec) = explode(" ", microtime());
$time = $sec.substr($usec,2); // 秒數(shù)+微秒數(shù)
$ext = substr($_FILES[$filename]['name'],strrpos($_FILES[$filename]['name'],'.')+1); // 上傳文件后綴
$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;
}
以上所述是小編給大家介紹的PHP實現(xiàn)多圖上傳和單圖上傳功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!