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.
Don't try to build this yourself. Use an existing class like http://pear.php.net/package/Archive_Tar to handle that for you.
The below code reads a file inside a .gz zip file
Note that you need to have the zip extension of php enabled for this code to work.
Use the zlib extension
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.
This works for gz, zip, tar and bz files.
You can do this with the
PharData
class:This even works with the
phar://
stream wrapper:If you don't have Phar, check the PHP-only implementation, or the pecl extension.