开发者

How to edit a file inside a zip package submitted from HTML form

开发者 https://www.devze.com 2023-03-28 12:53 出处:网络
I\'m trying to setup a html form that will take a file from a zip archive and edit it (change a few variables value) on submit.

I'm trying to setup a html form that will take a file from a zip archive and edit it (change a few variables value) on submit.

so here's the process:

user inputs details into the html form

a copy of a zip archive with a cofig.php will be copied into a temp folder

config.php will be extracted from the zip

once config.php has been extracted, the config.php in the zip will be deleted (ready for replacement)

the config.php that gets extracted needs to be edited

contents of config.php:

<?

$varible1 = "data_from_html_form";

$varible2 = "data_from_html_form";

$varible3 = "some_value";

//etc etc....

?>

the file will then be saved and placed back into the temporary zip archive ready to be distr开发者_StackOverflow中文版ibuted to the user.

All i need to know is how to edit the config.php and the variables in it.


Open a file with fopen to read your file into an array of strings, process it as you need (add/modify/remove), than use file_put_contents to save it back to file.

$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);

You can call file_put_contents with one-dimension array as second argument, but check if all newlines are still present in new file. If they are disappeared, use implode("\n",$contents) instead of just $contents.


It looks like all you're trying to do is open a file, prepaid a string, and write back to the file:

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

If that's not what you're trying to do, forgive my misunderstanding.

Note: not sure on the order of arguments, and I don't have time at the right now to look. You should be able to find that stuff on php.net.


Have you had a look here http://php.net/manual/en/wrappers.compression.php. Look at the examples. You will need the zip extension installed though http://www.php.net/manual/en/book.zip.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号