Skip to content

Commit bdbdb8b

Browse files
committed
Fixed reading .env file in BIN Downloader Script
1 parent fdd18a7 commit bdbdb8b

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ The command above will download the DATABASE_CODE BIN file and unzip the file in
128128
|token|Download token. You can get your token at your [IP2Location Account Area](https://www.ip2location.com/account) at the Download page.|
129129
|file|Database package. (DB1BIN...DB26BIN, DB1BINIPV6...DB26BINIPV6, DB1LITEBIN...DB11LITEBIN or DB1LITEBINIPV6...DB11LITEBINIPV6) You may login to your [IP2Location Account Area](https://www.ip2location.com/account) and get the package code (or download code) at the Download page. |
130130

131-
For linux user, you can set the **DOWNLOAD_TOKEN** and **DATABASE_CODE** with values in your .env environment and run the command `php ip2location_bin_download.php` for the download.
131+
You can set the **DOWNLOAD_TOKEN** and **DATABASE_CODE** with values in the .env file and run the command `php ip2location_bin_download.php` for the download.
132132

133133
## IPv4 BIN vs IPv6 BIN
134134
* Use the IPv4 BIN file if you just need to query IPv4 addresses.

ip2location_bin_download.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,40 @@
3333
$dbCode = $inputs["file"];
3434
}
3535

36+
$envFilePath = realpath(ROOT . ".env");
37+
$varArr = [];
38+
if (is_file($envFilePath)) {
39+
if (is_readable($envFilePath)) {
40+
$fopen = fopen($envFilePath, 'r');
41+
if ($fopen){
42+
while (($line = fgets($fopen)) !== false) {
43+
$commentLine = (substr(trim($line),0 , 1) == '#') ? true: false;
44+
if ($commentLine || empty(trim($line))) {
45+
continue;
46+
}
47+
$varLine = explode("#", $line, 2)[0];
48+
$envEx = preg_split('/(\s?)\=(\s?)/', $varLine);
49+
$envName = trim($envEx[0]);
50+
$envValue = isset($envEx[1]) ? trim($envEx[1]) : "";
51+
$varArr[$envName] = $envValue;
52+
}
53+
fclose($fopen);
54+
}
55+
}
56+
}
57+
3658
if ($token == '') {
37-
if (!empty(getenv('DOWNLOAD_TOKEN'))) {
38-
$token = getenv('DOWNLOAD_TOKEN');
59+
if (isset($varArr['DOWNLOAD_TOKEN'])) {
60+
$token = $varArr['DOWNLOAD_TOKEN'];
3961
} else {
4062
echo "[Error] Missing --token command line switch or parameter.\n";
4163
exit;
4264
}
4365
}
4466

4567
if ($dbCode == '') {
46-
if (!empty(getenv('DATABASE_CODE'))) {
47-
$dbCode = getenv('DATABASE_CODE');
68+
if (isset($varArr['DATABASE_CODE'])) {
69+
$dbCode = $varArr['DATABASE_CODE'];
4870
} else {
4971
echo "[Error] Missing --file command line switch or parameter.\n";
5072
exit;

0 commit comments

Comments
 (0)