true, 'info' => '' ); foreach ($params as $attr => $type) { $list = explode(',', $attr); foreach ($list as $item) { $item = trim($item); if($item != ''){ if (!isset($post[$item]) || $post[$item] == '') { $result['status'] = false; $result['info'] = '请填写所有必填项'; return $result; } elseif (zzcsUtils::validate($post[$item], $type)) { continue; } else { $result['status'] = false; $result['info'] = ' 数据格式不正确'; return $result; } } } } return $result; } /* * 验证字段是否合法,URL必须带http(s),合法return true,非法return false * @input mixed需要验证的参数 * @type 需要的方法,Number, Float, Integer, ID, Email, Phone, URL, ShenFenZheng, */ static function validate($input, $type = '') { $typeArr = explode(',', $type); foreach ($typeArr as $type) { switch (strtolower($type)) { case 'amount': $reg = '/^([1-9]\d*\.?\d{0,2}$)|0\.?\d{0,2}$/'; $message = '金额'; break; case 'number': $reg = '/^-?[0-9]*$/'; $message = '数字'; break; case 'email': $reg = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/'; $message = '邮箱'; break; case 'telorphone': //正则不正确,需再完善 $reg = '/^(d{3}-\d{8}|\d{4}-\d{7})|((0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8})$/'; $message = '电话'; break; case 'tel': $reg = '/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/'; $message = '手机'; break; case 'phone': $reg = '/^d{3}-\d{8}|\d{4}-\d{7}$/'; $message = '电话'; break; case 'url': $reg = '/^https?:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])$/i'; $message = '链接'; break; case 'card': $reg = '/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/'; $message = '身份证'; break; case 'dcode': $reg = '/^[1-9]\d{5}(?!\d)$/'; $message = '邮编'; break; case 'time': $reg = '/^(([0-1][0-9])|(2[0-3])):([0-5][0-9])$/'; $message = '时间'; break; case 'empty': default: $reg = '/^[\s\S]*$/'; } $result = preg_match($reg, $input); if ($result == 0) return false; } return true; } /* * 上传多张图片,不确定图片张数, * 图片路径以逗号分隔, * */ static function uploadMultiple($name, $dirPath = '../coding/resource/upload/car/', $realPath = '../../resource/upload/car/') { $maxSize = 2000000; $allow = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg'); $path = ''; $type = ''; if (is_array($_FILES[$name]['name'])) { //多个文件 foreach ($_FILES[$name]['name'] as $key => $item) { //图片不为空,且图片对应的类型也不为空 if ($_POST['bus_img_type'][$key] != '' && $item != '' && in_array($_FILES[$name]['type'][$key], $allow) && $_FILES[$name]['size'][$key] <= $maxSize) { $newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$name]['name'][$key])); move_uploaded_file($_FILES[$name]["tmp_name"][$key], __DIR__ . DIRECTORY_SEPARATOR . $dirPath . $newName); if ($path == '') { $path .= $realPath . $newName; $type .= $_POST['bus_img_type'][$key]; } else { $path .= ',' . $realPath . $newName; $type .= ',' . $_POST['bus_img_type'][$key]; } } } } else { //单个文件 } if ($path != '') return array('path' => $path, 'type' => $type); else return false; } /* * 上传文件 * @type 上传类型,主要是图片,还有附件rar, word, excel,还没有需求 */ static function upload($type, $file) { if ((($_FILES[$file]["type"] == "image/gif") || ($_FILES[$file]["type"] == "image/jpeg") || ($_FILES[$file]["type"] == "image/png") || ($_FILES[$file]["type"] == "image/pjpeg")) && ($_FILES[$file]["size"] < 200000) ) { if ($_FILES[$file]["error"] > 0) { return ''; } else { $path = '../upload/car/'; $newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$file]["name"])); move_uploaded_file($_FILES[$file]["tmp_name"], __DIR__ . DIRECTORY_SEPARATOR . $path . $newName); return $path . $newName; } } else { return ''; } } /* * 在upload目录上传文件时,初始化一个不存在的文件名 */ static function initFileName($path, $suffix) { $flag = true; while ($flag) { $name = time() . rand(10000, 99999); if (!file_exists("$path$name$suffix")) { $flag = false; } } return $name . $suffix; } static function getSuffix($imgName)//获取图像文件类型 { if (preg_match("/\.(jpg|jpeg|gif|png|bmp)$/i", $imgName, $matches)) { $type = strtolower($matches[0]); } else { $type = "string"; } return $type; } /* * 删除车辆之前存放的图片 */ static function deleteFiles($oldPath, $newPath) { if (is_string($oldPath)) $oldPath = explode(',', $oldPath); if (is_string($newPath)) $newPath = explode(',', $newPath); foreach ($oldPath as $item) { if (!in_array($item, $newPath)) { if (file_exists($item) && is_file($item)) unlink($item); } } } }