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

6

15

I am building a system for people to upload .tar (and .tar.gz, .tar.bz2, .zip, etc) files in PHP. Uploading the files is fine, but I would like to list files contained in the archive after it has been uploaded.

Can someone recommend a good PHP library that can read file archives?

I found File_Archive on Pear but it hasn't been updated in a few years. ZipArchive works great for .zip files, but I need something that can handle more file types.

update I'm running on RHEL6, PHP 5.2, and Apache 2.2.

Crapshooter answered 2/2, 2011 at 19:14 Comment(2)
What operating system are you running ?Foeticide
@Foeticide RHEL6 means Red Hat Enterprise Linux Version 6Claudette
W
32

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.

Wicker answered 2/2, 2011 at 19:20 Comment(1)
seems to be a problem with certain types of tar #37190187Wowser
S
5

Don't try to build this yourself. Use an existing class like http://pear.php.net/package/Archive_Tar to handle that for you.

Striker answered 2/2, 2011 at 19:23 Comment(2)
Does Archive_Tar handle compressed files? And the documentation doesn't say it can give a list of the files in a .tar file. I don't want to extract the files, I just want to list their contents.Crapshooter
@Michael: Yes, it supports both .gz and .bz2 compression. And there is a ->listContent method among others.Striker
C
4

The below code reads a file inside a .gz zip file

    <?php
    $z = gzopen('zipfile.gz','r') or die("can't open: $php_errormsg");
    $string = '';

    while ($line = gzgets($z,1024)) {
        $string .= $line;
    }

    echo $string;

    gzclose($z) or die("can't close: $php_errormsg");
    ?>

Note that you need to have the zip extension of php enabled for this code to work.

Centaurus answered 21/9, 2012 at 6:57 Comment(1)
Excellent. This helped me.Drewdrewett
V
3

I don't think the first answer works. Or it only doesn't work for me. You could not read file content when you foreach it. I give my working code below.

$fh = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('phar:///dir/file.tar.gz'),
    RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($fh as $splFileInfo) {
    echo file_get_contents($splFileInfo->getPathname());
}

This works for gz, zip, tar and bz files.

Vogler answered 27/5, 2015 at 7:24 Comment(0)
C
2

Use the zlib extension

Cervin answered 2/2, 2011 at 19:18 Comment(2)
Does the zlib extension handle TAR files?Crapshooter
No, it only handles the gz compression/decompression. You already have better answers to handle the tar side of your problem.Cervin
L
-1

How about sticking with the original tar library (if pre-installed)?

echo shell_exec('tar -tf <path/to/file.tgz>');// lists the contents of a Gzipped Tarball
Linebacker answered 15/9, 2023 at 18:36 Comment(1)
Regarding downvotes, I do believe that since you have enough reputation to vote, adding some comments about the solution that works fine on at least all major Linux systems btw is always better than a silent and anonymous downvote.Linebacker

© 2022 - 2024 — McMap. All rights reserved.