Skip to content

Commit 35d5ab6

Browse files
committed
Try curl when allow_url_fopen is disabled
1 parent 9e92721 commit 35d5ab6

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Services/MaxMindDatabase.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function update()
7373
$this->withTemporaryDirectory(function ($directory) {
7474
$tarFile = sprintf('%s/maxmind.tar.gz', $directory);
7575

76-
file_put_contents($tarFile, fopen($this->config('update_url'), 'r'));
76+
$this->downloadFileByUrl($tarFile, $this->config('update_url'));
7777

7878
$archive = new PharData($tarFile);
7979

@@ -166,4 +166,26 @@ protected function deleteDirectory(string $directory)
166166

167167
return rmdir($directory);
168168
}
169+
170+
protected function downloadFileByUrl(string $filename, string $url): void
171+
{
172+
$canUseFopenForUrl = in_array(strtolower((string) ini_get('allow_url_fopen')), ['1', 'on'], true);
173+
if ($canUseFopenForUrl) {
174+
file_put_contents($filename, fopen($url, 'rb'));
175+
} elseif (extension_loaded('curl')) {
176+
$fp = fopen($filename, 'wb+');
177+
if ($fp === false) {
178+
throw new \RuntimeException("Cannot open $filename file for writing.");
179+
}
180+
$ch = curl_init();
181+
curl_setopt($ch, \CURLOPT_URL, $url);
182+
curl_setopt($ch, \CURLOPT_FILE, $fp);
183+
curl_setopt($ch, \CURLOPT_FOLLOWLOCATION, true);
184+
curl_exec($ch);
185+
curl_close($ch);
186+
fclose($fp);
187+
} else {
188+
throw new \RuntimeException('Cannot download the file. Please enable allow_url_fopen or install curl extension.');
189+
}
190+
}
169191
}

0 commit comments

Comments
 (0)