当前位置:网站首页>框架外的PHP读取.env文件(php5.6、7.3可用版)
框架外的PHP读取.env文件(php5.6、7.3可用版)
2022-08-11 08:17:00 【德宏大魔王】
背景:由于项目需要,在框架外(public文件夹下)新建了一个原生的php文件,由于不知道怎么解决
“框架外的php文件,怎么使用DB操作”
于是上网搜索了一早上也没能又方法
于是我使用了,原生连接方法
//连接数据库
$conn=mysqli_connect('localhost','demo','3KHC999zLX666','demo') or die("連接失敗");
// $conn = new PDO('mysql:host=localhost;dbname=xcx_club', 'xcx_m_club', 'yyyyyy');
mysqli_query($conn, "set names utf8");

后来由于客户说,最好就是指定一个文件,用env的就行,我上网找了很多,最终解决办法如下:
(我的环境 nginx php7,3)
.env文件
APP_DEBUG = true
[APP]
DEFAULT_TIMEZONE = Asia/Taipei
[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = booking885_com
USERNAME = booking885_com
PASSWORD = 3KHC26dzLXd222
HOSTPORT = 3306
CHARSET = utf8mb4
DEBUG = true
authcode = j1g8KSrkbPLts9rNiM
[LANG]
default_lang = zh-cn
index.php文件
<?php
class Env
{
const ENV_PREFIX = 'PHP_';
/**
* 加载配置文件
* @access public
* @param string $filePath 配置文件路径 - php7+以上加string
* @return void - php7+才支持
*/
public static function loadFile($filePath)//:void
{
if (!file_exists($filePath)) throw new \Exception('配置文件' . $filePath . '不存在');
//返回二位数组
$env = parse_ini_file($filePath, true);
foreach ($env as $key => $val) {
$prefix = static::ENV_PREFIX . strtoupper($key);
if (is_array($val)) {
foreach ($val as $k => $v) {
$item = $prefix . '_' . strtoupper($k);
putenv("$item=$v");
}
} else {
putenv("$prefix=$val");
}
}
}
/**
* 获取环境变量值
* @access public
* @param string $name 环境变量名(支持二级 . 号分割)
* @param string $default 默认值
* @return mixed
*/
public static function get($name, $default = null)
{
$result = getenv(static::ENV_PREFIX . strtoupper(str_replace('.', '_', $name)));
if (false !== $result) {
if ('false' === $result) {
$result = false;
} elseif ('true' === $result) {
$result = true;
}
return $result;
}
return $default;
}
}
Env::loadFile("../../.env"); /*调用配置文件*/
$app_api_url = Env::get('database.hostname'); /*调用变量方法*/
var_dump($app_api_url);
echo($app_api_url);
这里如果出现这样的问题
只需要在宝塔上的php相应的版本上把禁用函数去除即可

在访问即可
其他的参数自己根据代码获取,代码简单,自己读吧
最后
️️️️️️ 🥳🥳🥳 茫茫人海千千万万,感谢这一刻你看到了我的文章,感谢观赏,大家好呀,欢迎加入人工智能交流群(看我的动态),更多周边福利等你🥳🥳🥳
欢迎订阅本专栏或者关注我,大家一起努力每天一题算法题
️️️ 最后,希望我的这篇文章能对你的有所帮助!
愿自己还有你在未来的日子,保持学习,保持进步,保持热爱,奔赴山海! ️️️
边栏推荐
- Two startup methods and differences of Service
- Kotlin算法入门兔子数量优化及拓展
- 零基础SQL教程: 基础查询 05
- 借问变量何处存,牧童笑称用指针,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang类型指针(Pointer)的使用EP05
- Write a resume like this, easy to get the interviewer
- JRS303-数据校验
- 机器学习(二)线性回归
- Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
- 抽象类和接口
- Openlayers Aggregate Graph, Weight Aggregate Graph, and Aggregate Graph Click Events
猜你喜欢

My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful

JRS303-数据校验

The growth path of a 40W test engineer with an annual salary, which stage are you in?

C Primer Plus(6) 中文版 第1章 初识C语言 1.7 使用C语言的7个步骤

装饰器模式:Swift 实现

C语言操作符详解

Write a resume like this, easy to get the interviewer

Do you know the basic process and use case design method of interface testing?

golang 字符串操作

CSDN21天学习挑战赛——封装(06)
随机推荐
CIKM 2022 AnalytiCup Competition: Federal Heterogeneous Task Learning
囍楽cloud task source code
Getting Started with Kotlin Algorithms Calculating Prime Factors
老干妈创始人陶华碧现身直播间,70岁“国民女神”拥抱直播电商
Kotlin算法入门求回文数数算法优化二数字生成规则
Filesystem Hierarchy Standard
【LeetCode】链表题解汇总
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
用 Antlr 重构脚本解释器
抽象类和接口
Test cases are hard?Just have a hand
8、Mip-NeRF
优炫数据库支持多列分区吗?
借问变量何处存,牧童笑称用指针,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang类型指针(Pointer)的使用EP05
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
流式结构化数据计算语言的进化与新选择
RestTemplate工具类
3.1-Classification-probabilistic generative model
redis operation
高德能力API
