public static function GetID($length, $table) { $id = Util_String::GenRandomStr($length, Util_String::CHAR_NUM); while (self::$idArray[$id]) { if ($length <= 5) { //保证程序健壮性 防止死循环 return false; } $id = Util_String::GenRandomStr($length, Util_String::CHAR_NUM); } if (DB::Exists($table, array('id' => $id))) { return false; } self::$idArray[$id] = 1; return $id; }
/** * 文件上传 * */ public static function Save($uploadFileKey, $prefix = '', $saveFolder = '', $savePath = '') { $file = $_FILES[$uploadFileKey]; if (!$file) { System::AddError("文件上传失败,没有文件", System::MESSAGE_SYS); return false; } $error = $file['error']; $tmpName = $file['tmp_name']; if ($error != UPLOAD_ERR_OK) { System::AddError("文件上传失败,错误码:" . $error, System::MESSAGE_SYS); return false; } $maxSize = self::$max_upload_size; if ($file['size'] > self::$max_upload_size * 1024 * 1024) { System::AddError("文件过大,最大不得超过{$maxSize}M:", System::MESSAGE_SYS); return false; } if (!$savePath) { $fileNames = explode('.', $file['name']); $fileName = md5($fileNames[0]); $fileName = $prefix . $fileName . Util_String::GenRandomStr(6); $countNamesCount = count($fileNames); if ($countNamesCount > 1) { $fileName .= ".{$fileNames[$countNamesCount - 1]}"; } $fileName = strtolower($fileName); $folder = self::GetDefaultFolder($file['type'], $saveFolder); if (!$folder) { return false; } $savePath = $folder . '/' . $fileName; } $result = move_uploaded_file($tmpName, $savePath); if (!$result) { System::AddError("文件保存失败~"); return false; } $count = strlen(ROOT_PATH); $savePath = substr($savePath, $count); return $savePath; }
public static function dbRandomId($pkName = 'id') { $pkName = $pkName ? $pkName : 'id'; $randomId = Util_String::GenRandomStr(8, Util_String::CHAR_NUM); $db = self::dbObject(); if ($db->exsits(array($pkName => $randomId), $pkName)) { return null; } else { return $randomId; } }