Open the .gz file and read / replace

I can not find the answer that I need, so I hope you understand what I need.

I need to open the tar.gz file (from a remote site), read the files, and copy / replace these files on my website. I tried this code (as well as many other codes):

$zipFile= 'http://awebsite.com/file.tar.gz';
$dir = 'myfolder';
$zp = @gzopen($zipFile, "r");
$fp = @fopen($dir, "w");
while(!@gzeof($zp)) {$string = @gzread($zp, 4096); @fwrite($fp, $string, strlen($string));}
@gzclose($zp);
@fclose($fp);

Everyone seems to be coming up with all sorts of mistakes. It seems I can read the zip file, but I don’t actually save the content on my website.

Thanks for any help in advance.

Creton

+3
source share
2 answers

Try the following:

<?php
$zipFile= 'http://awebsite.com/file.tar.gz';
$dir = 'myfolder';
$zp = @gzopen($zipFile, "r");
$fp = @fopen("temp.tar", "w");
while(!@gzeof($zp)) {$string = @gzread($zp, 4096); @fwrite($fp, $string, strlen($string));}
@gzclose($zp);
@fclose($fp);
exec('tar xf temp.tar --overwrite --directory='.$dir);
?>


Update:

Here is a solution without using a temporary file and tarhandles gzip decompression itself:

<?php
$zipFile= 'http://awebsite.com/file.tar.gz';
$dir = 'myfolder';
$zp = @fopen($zipFile, "r");
$fp=popen('tar xzf - --overwrite --directory='.$dir,'w');
while(!@feof($zp)) {$string = @fread($zp, 4096); @fwrite($fp, $string, strlen($string));}
@fclose($zp);
@fclose($fp);
?>
+1
source

, .gz, tar , .tar. - tar. , tar, , . tar man, , . ( .)

+3

All Articles