Conditions | 6 |
Paths | 6 |
Total Lines | 22 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
15 | public static function downloadURL(string $url, string $outfile, ?callable $callback = null) : void |
||
16 | { |
||
17 | if (($rh = fopen($url, 'rb')) === false) { |
||
18 | throw DownloadException::downloadError('Error opening file: ' . $url); |
||
19 | } |
||
20 | |||
21 | if(($wh = fopen($outfile, 'wb')) === false){ |
||
22 | throw DownloadException::downloadError('Error creating file: ' . $outfile); |
||
23 | } |
||
24 | |||
25 | $kbRead = 0; |
||
26 | while (!feof($rh)) { |
||
27 | $kbRead++; |
||
28 | if (fwrite($wh, fread($rh, 1024)) === false) { |
||
29 | throw DownloadException::downloadError('Cannot write to file: ' . $outfile); |
||
30 | } |
||
31 | if ($callback !== null) { |
||
32 | $callback($kbRead); |
||
33 | } |
||
34 | } |
||
35 | fclose($rh); |
||
36 | fclose($wh); |
||
37 | } |
||
39 |