Sami
Sami

Reputation: 1491

PHP - file_get_contents() of a file inside a subdirectory in a zip file

I have a .zip file with multiple subdirectories and files inside them. How to use file_get_contents() to get the content of a file inside one of the subdirectories of the zip file?

Upvotes: 3

Views: 4289

Answers (2)

iainn
iainn

Reputation: 17434

If you've got the Zip extension installed, it should have registered the zip:// compression wrapper. Try the following:

$result = file_get_contents('zip://path_to_my.zip#subDir/foo.txt');

Upvotes: 5

BorysekOndrej
BorysekOndrej

Reputation: 91

There are two distinct ways to do this. One is to use to use Zip wrapper like this:

$result = file_get_contents('zip://foo.zip#dirName/bar.txt');

Update: The following doesn't seem to work now, not sure yet why. Or you can use Zip Archive class like this:

$zipFile = new ZipArchive();
if ($zipFile->open(dirname(__FILE__) . '/foo.zip')) {
    $string = $zipFile->getFromName("dirName/bar.txt");
}

Upvotes: 2

Related Questions