I have three files 1.txt, 2.txt and 3.txt in a tar.gz file named info.tar.gz. How to get the file names (1.txt, 2.txt, 3.txt) from info.tar.gz by PHP? and how to get the content in the 3 text files? Thanks in advance!
Asked
Active
Viewed 1,928 times
1
-
http://stackoverflow.com/questions/4878792/how-can-i-read-a-tar-gz-file-with-php – nowhere Nov 19 '13 at 13:54
-
Maybe this answer will help you [PHP Untar-gz without exec()?](http://stackoverflow.com/questions/9416508/php-untar-gz-without-exec) ...given you have PHP >= 5.3 – Havelock Nov 19 '13 at 13:54
1 Answers
1
There is a Phar
extension for PHP which can be used. If you don't have that extension installed (and don't have permissions to install) you can use shell_exec()
:
$files = explode(PHP_EOL, shell_exec('tar -ztvf info.tar.gz'));
var_dump($files);

hek2mgl
- 152,036
- 28
- 249
- 266
-
By this I get the files name, can you please also show get content and files name using Phar? I tried://$tar = new PharData('info.tar.gz'); $tar->decompress(); $files = new \PharData('info.tar'); $files->extractTo('/full/path/'); – user2241859 Nov 20 '13 at 15:50
-
I gave you this link: http://stackoverflow.com/questions/4878792/how-can-i-read-a-tar-gz-file-with-php ... Should work – hek2mgl Nov 20 '13 at 15:52