class UploadFile { public static function uploadCoverPic($savePath, $uniPrefix = 'O_', $upname, $index = '') { if (is_int($index)) { $file_error = $_FILES[$upname]['error'][$index]; $file_name = $_FILES[$upname]['name'][$index]; $file_size = $_FILES[$upname]['size'][$index]; $file_tmp_name = $_FILES[$upname]['tmp_name'][$index]; } else { $file_error = $_FILES[$upname]['error']; $file_name = $_FILES[$upname]['name']; $file_size = $_FILES[$upname]['size']; $file_tmp_name = $_FILES[$upname]['tmp_name']; } if ($file_error != 0) { return array('code' => xxx, 'error' => 'error'); } else { $info = pathinfo($file_name); $ext = strtolower($info['extension']); if (!in_array($ext, array('jpg', 'jpeg', 'bmp', 'png'))) { return array('code' => xxx, 'error' => '上传文件类型不允许'); } if ($file_size > 5242880) { return array('code' => xxx, 'error' => '上传文件大小不符!'); } if (!$file_tmp_name) { return array('code' => xxx, 'error' => '非法上传文件!'); } } if(!is_dir($savePath)) { mkdir($savePath, 0777, true); } $destination = $savePath . uniqid($uniPrefix) . '_' . mt_rand(1111,9999). '.jpg'; move_uploaded_file($file_tmp_name, $destination); return array('code' => 200, 'dest' => $destination); }
原生PHP上传封装类
最新推荐文章于 2021-03-29 12:09:55 发布