src/Console/CommandTrait.php
protected function downloadFile($name, $update = false)
    {
        $url = $this->files[$name]['url'];
        $storagePath = config('geonames.storagePath');
        $txtFileName = $this->files[$name]['filename'] . '.txt';
        $urlSize = $this->getUrlSize($url);
        $urlFileName = basename($url);

        $fileSize = @filesize($storagePath . '/' . $urlFileName);
        if ($fileSize) {
            if ($fileSize === $urlSize) {
                $this->line('<info>File Exists:</info> ' . $urlFileName . ' with same size as remote geonames file exists.');
                return true;
            } else if ($fileSize !== $urlSize && !$update) {
                $this->line('<info>File Exists:</info> ' . $urlFileName . ' with different size as remote geonames file exists. You should consider downloading newest files.');
                return true;
            } else {
                // If we are here, we will re-download zip file, so it is worthwhile to remoeve the old txt version first
                $extractedFilePath = $storagePath . '/' . $txtFileName;
                if (substr($urlFileName, -4) === '.zip' && file_exists($extractedFilePath)) {
                    $this->line('<info>Removing Old File:</info> ' . basename($extractedFilePath));
                    unlink($extractedFilePath);
                }
            }
        }

        // Create download directory
        if (!file_exists($storagePath)) {
            mkdir($storagePath, 0755, true);
        }

        // Create .gitignore if it does not exist in $storagePath
        if (!file_exists($storagePath.'/.gitignore')) {
            $gitignore = "*\n!.gitignore\n";
            file_put_contents($storagePath.'/.gitignore',$gitignore);
        }

        // Open file and truncate it for writing (requires fopen_wrappers)
        $targetFile = $storagePath . '/' . $urlFileName;
        $targetFP = fopen($targetFile, 'w+');
        if ($targetFP === false) {
            throw new RuntimeException('Can not open file for writing: ' . $targetFile);
        }

        $sourceFP = fopen($url, 'r');
        if ($sourceFP === false) {
            throw new RuntimeException('Can not open file for reading: ' . $url);
        }

        $bufferSize = 1024 * 1024;
        // Size should be at leaast 1 to show remaining time...
        $steps = $urlSize / $bufferSize + 1;
        /* @var $output OutputStyle */
        $output = $this->getOutput();
        $bar = $output->createProgressBar($steps);
        $bar->setFormat('<info>Downloading:</info> ' . $urlFileName . ' %bar% %percent%% <info>Remaining Time:</info> %remaining%');
        while (!feof($sourceFP)) {
            fwrite($targetFP, stream_get_contents($sourceFP, $bufferSize));
            $bar->advance();
        }
        $bar->finish();
        $output->newLine();

        clearstatcache(true, $targetFile);
        $this->line('<info>File Downloaded:</info> ' . $urlFileName . ' - ' . filesize($targetFile) . ' bytes.');

        return true;
    }
protected function downloadFile($name, $update = false)
    {
        $url = $this->files[$name]['url'];
        $storagePath = config('geonames.storagePath');
        $txtFileName = $this->files[$name]['filename'] . '.txt';
        $urlSize = $this->getUrlSize($url);
        $urlFileName = basename($url);

        $fileSize = @filesize($storagePath . '/' . $urlFileName);
        if ($fileSize) {
            if ($fileSize === $urlSize) {
                $this->line('<info>File Exists:</info> ' . $urlFileName . ' with same size as remote geonames file exists.');
                return true;
            } else if ($fileSize !== $urlSize && !$update) {
                $this->line('<info>File Exists:</info> ' . $urlFileName . ' with different size as remote geonames file exists. You should consider downloading newest files.');
                return true;
            } else {
                // If we are here, we will re-download zip file, so it is worthwhile to remoeve the old txt version first
                $extractedFilePath = $storagePath . '/' . $txtFileName;
                if (substr($urlFileName, -4) === '.zip' && file_exists($extractedFilePath)) {
                    $this->line('<info>Removing Old File:</info> ' . basename($extractedFilePath));
                    unlink($extractedFilePath);
                }
            }
        }

        // Create download directory
        if (!file_exists($storagePath)) {
            mkdir($storagePath, 0755, true);
        }

        // Create .gitignore if it does not exist in $storagePath
        if (!file_exists($storagePath.'/.gitignore')) {
            $gitignore = "*\n!.gitignore\n";
            file_put_contents($storagePath.'/.gitignore',$gitignore);
        }

        // Open file and truncate it for writing (requires fopen_wrappers)
        $targetFile = $storagePath . '/' . $urlFileName;
        $targetFP = fopen($targetFile, 'w+');
        if ($targetFP === false) {
            throw new RuntimeException('Can not open file for writing: ' . $targetFile);
        }

        $sourceFP = fopen($url, 'r');
        if ($sourceFP === false) {
            throw new RuntimeException('Can not open file for reading: ' . $url);
        }

        $bufferSize = 1024 * 1024;
        // Size should be at leaast 1 to show remaining time...
        $steps = $urlSize / $bufferSize + 1;
        /* @var $output OutputStyle */
        $output = $this->getOutput();
        $bar = $output->createProgressBar($steps);
        $bar->setFormat('<info>Downloading:</info> ' . $urlFileName . ' %bar% %percent%% <info>Remaining Time:</info> %remaining%');
        while (!feof($sourceFP)) {
            fwrite($targetFP, stream_get_contents($sourceFP, $bufferSize));
            $bar->advance();
        }
        $bar->finish();
        $output->newLine();

        clearstatcache(true, $targetFile);
        $this->line('<info>File Downloaded:</info> ' . $urlFileName . ' - ' . filesize($targetFile) . ' bytes.');

        return true;
    }
Download a file if it does not exist

@param String $url Download URL
@param String $path Storage path
@param Boolean $force Force re-download of files

@return boolean