-1

I have a specific file in a .tar.gz archive that I'd like to extract. How do I do this in PHP?

For example, MaxMind's GeoLite databases are available as a .tar.gz files. For each product, the filename is GeoLite2-ProductName.tar.gz, inside there's a GeoLite2-ProductName_yyymmdd directory, and inside that, a GeoLite2-ProductName.mmdb file along with a few other text files. I only want to extract the .mmdb file.

Tyler
  • 161
  • 1
  • 11

1 Answers1

0

Using PHP phar, open the .tar.gz archive, iterate through the files, and extract only the filename you're interested in.

$phar = new \PharData('/var/www/html/GeoLite2-ProductName.tar.gz');

foreach (new \RecursiveIteratorIterator($phar) as $file) {
    if (str_ends_with($file, 'GeoLite2-ProductName.mmdb')) {
        $contents = file_get_contents($file->getPathName());
        file_put_contents('/var/www/html/GeoLite2-ProductName.mmdb', $contents);
        break;
    }
}
Tyler
  • 161
  • 1
  • 11