@@ -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