在zip或压缩文件中写入文件

时间:2014-12-22 17:59:54

标签: php zip compression

我对此没有任何反应,我在此提出了我的问题

例如,如果我在zip文件或其他类型的压缩文件中有一个包含此内容的文件:

<?php
$name="Jhon";
$phone="123456789";
$city="London";
?>

我想要例如访问此文件并从表单写入以更改一些信息,也可以从zip文件或压缩文件,tar等读取所有内容

此外,我想在此zip文件中包含此文件,以获取信息

有可能用php或其他方面做到这一点吗?

谢谢,最好的问候

1 个答案:

答案 0 :(得分:0)

您可以解压缩文件,进行更改,然后将其重新添加回存档(假设您已安装ZipArchive类)。

$zip = New \ZipArchive;
$res = $zip->open('yourarchive.zip');
if (true === $res) 
{
    $zip->extractTo('/my/destination/dir/', 'test.php');

    $contents = file('*your filename*');
    foreach ($contents as $line) 
    {
        //perform alterations;
    }

    unset ($contents[2]); //remove line 2;
    $contents[] = "aaaa"; //append "aaaa" to the end of the file

    file_put_contents('test.php', $contents);

    // do your edits to the file
    $zip->addFile('test.php');
    $zip->close();
}