如何编辑从HTML表单提交的zip包中的文件

时间:2011-08-17 12:35:47

标签: php html

我正在尝试设置一个html表单,该表单将从zip存档中获取文件并在提交时编辑它(更改一些变量值)。

所以这是过程:

用户将详细信息输入到html表单

带有cofig.php的zip存档副本将被复制到临时文件夹

将从zip

中提取config.php

一旦提取了config.php,zip中的config.php将被删除(准备更换)

需要编辑提取的config.php

config.php的内容:

<?

$varible1 = "data_from_html_form";

$varible2 = "data_from_html_form";

$varible3 = "some_value";

//etc etc....

?>

然后将保存文件并将其放回临时zip存档中,准备分发给用户。

我需要知道的是如何编辑config.php及其中的变量。

3 个答案:

答案 0 :(得分:0)

看起来你要做的就是打开一个文件,预付一个字符串,然后写回文件:

$phpfile = file_get_contents($filename);
$phpfile .= $prependdata;
file_put_contents($phpfile, $filename);

如果那不是你想要做的,请原谅我的误解。

注意:不确定参数的顺序,我现在没有时间看。你应该能够在php.net上找到那些东西。

答案 1 :(得分:0)

使用fopen打开文件,将文件读入字符串数组,根据需要进行处理(添加/修改/删除),而不是使用file_put_contents将文件保存回文件。 / p>

$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('*filename*', $contents);

您可以使用一维数组作为第二个参数调用file_put_contents,但检查新文件中是否仍然存在所有换行符。如果它们消失,请使用implode("\n",$contents)而不是$contents

答案 2 :(得分:0)

你有没看过这里http://php.net/manual/en/wrappers.compression.php。看看这些例子。您需要通过http://www.php.net/manual/en/book.zip.php

安装zip扩展程序