当前位置:网站首页>框架外的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相应的版本上把禁用函数去除即可

在访问即可
其他的参数自己根据代码获取,代码简单,自己读吧
最后
️️️️️️ 🥳🥳🥳 茫茫人海千千万万,感谢这一刻你看到了我的文章,感谢观赏,大家好呀,欢迎加入人工智能交流群(看我的动态),更多周边福利等你🥳🥳🥳
欢迎订阅本专栏或者关注我,大家一起努力每天一题算法题
️️️ 最后,希望我的这篇文章能对你的有所帮助!
愿自己还有你在未来的日子,保持学习,保持进步,保持热爱,奔赴山海! ️️️
边栏推荐
- C Primer Plus(6) 中文版 第1章 初识C语言 1.6 语言标准
- TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
- Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
- 【415. 字符串相加】
- Distributed Lock-Redission - Cache Consistency Solution
- 【LeetCode】链表题解汇总
- 1071 Small Gamble (15 points)
- C语言操作符详解
- 3.2 - classification - Logistic regression
- 关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
猜你喜欢

装饰器模式:Swift 实现

9、Neural Sparse Voxel Fields

Project 1 - PM2.5 Forecast

Redis 只会用缓存?20种妙用让同事直呼牛X(荣耀典藏版)

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

JUC Concurrent Programming

2022-08-10:为了给刷题的同学一些奖励,力扣团队引入了一个弹簧游戏机, 游戏机由 N 个特殊弹簧排成一排,编号为 0 到 N-1, 初始有一个小球在编号 0 的弹簧处。若小球在编号为 i 的弹

Features of LoRa Chips

C语言操作符详解

The easiest trick to support quick renaming of various files
随机推荐
1.1-Regression
【C语言】每日一题,求水仙花数,求变种水仙花数
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
Creo9.0 特征的成组
Mysql JSON对象和JSON数组查询
go-grpc TSL authentication solution transport: authentication handshake failed: x509 certificate relies on ... ...
分布式锁-Redission - 缓存一致性解决
Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
Dynamic Agent Learning
Two startup methods and differences of Service
C语言-结构体
Kotlin算法入门兔子数量优化及拓展
CIKM 2022 AnalytiCup Competition: Federal Heterogeneous Task Learning
第一次因没有找到iframe元素而怀疑selenium4是不是有bug?
【Day_13 0509】▲跳石板
Write a resume like this, easy to get the interviewer
租房小程序
Four operations in TF
链式编程注解
My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
