How can I read a .tar.gz file with PHP?

You can do this with the PharData class:

// Example: list files
$archive = new PharData('/some/file.tar.gz');
foreach($archive as $file) {
        echo "$file\n";
}

This even works with the phar:// stream wrapper:

$list = scandir('phar:///some/file.tar.gz');
$fd = fopen('phar:///some/file.tar.gz/some/file/in/the/archive', 'r');
$contents = file_get_contents('phar:///some/file.tar.gz/some/file/in/the/archive');

If you don’t have Phar, check the PHP-only implementation, or the pecl extension.

Leave a Comment