How can I read a .tar.gz file with PHP?
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.
Solution 1:
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.
Solution 2:
Don't try to build this yourself. Use an existing class like http://pear.php.net/package/Archive_Tar to handle that for you.
Solution 3:
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.
Solution 4:
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.