tomfmason Posted July 11, 2006 Share Posted July 11, 2006 I am attempting to create a script that, when I purchase a new domain or add a subdomain, will update several files. I need to be able edit a specific line in a file.here is an example of what I have to write a zonefile[code=php:0]<?php$filename = 'C:\\windows\\system32\\dns\\etc\mysite1.com.zone';$handle = fopen($filename, "x+");$somecontent = "\$TTL 900 @ SOA ns1 ( postmaster 2004041700 21600 1800 604800 900 @ NS ns1 @ NS ns2 @ A xx.xx.xxx.xx @ MX 10 mail ns1 A xx.xx.xxx.xx ns2 A xx.xx.xxx.xx mail A xx.xx.xxx.xx www A xx.xx.xxx.xx";fwrite($handle, $somecontent);fclose($handle);?> [/code]Now if I want to add a cname I will need to open this file and edit a specific line. I would also want to do this with my named.conf and httpd.conf here is an example:say I want to add a domain, I will need to create a zone file and add 4 lines to my named.conf lines 89 through 97 [code]zone "mysite1.com" IN { type master; file "mysite1.com.zone";};zone "mysite2.com" IN { type master; file "mysite2.com.zone";};[/code]So when I create the zone file for my new domain(mysite3.com) I will need to add 4 more lines after line 97. Line 97 is not the last line in the file so I need to be able to add the info to a specific line. The same thing applies. Any suggestions would be great. Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/ Share on other sites More sharing options...
tomfmason Posted July 11, 2006 Author Share Posted July 11, 2006 I tried this[code=php:0]<?php$file = file('C:\\windows\\system32\\dns\\etc\\test_named.conf');$filename = 'C:\\windows\\system32\\dns\\etc\\test_named.conf';$lastsite = "mysite2.com";$lastzone = "mysite2.com.zone";$layout = "zone \"$lastsite\" IN { type master; file \"$lastzone\";};";$handle = fopen($filename, "w+b");$newdomain = "mysite3.com";$newzone = "mysite3.com.zone";$somecontent = "zone \"$newdomain\" IN { type master; file \"$newzone\";};"; foreach($file as $line_number => $content) { if (trim($content) == $layout) { fwrite($handle, $somecontent); fclose($handle); }}?>[/code]here is the contents of test_named.conf[code]zone "mysite.com" IN { type master; file "mysite.com.zone";};zone "mysite2.com" IN { type master; file "mysite2.com.zone";};zone "xxx.xx.xx.in-addr.arpa" { type master; file "xx.xx.xxx.rev";};[/code] it is not writing, it is [b]erasing[/b] all of the content in test_named.conf . This only happens if I use "w", "w+", "wb", or "w+b" . If I use any variation of "r" nothing happens.Anyone have any suggestions as to why this is happening or how I can fix it? Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/#findComment-56112 Share on other sites More sharing options...
ShogunWarrior Posted July 11, 2006 Share Posted July 11, 2006 I think the best thing to do is read the whole file into a string/array then edit it and then write it back out to the file.W,W+,WB etc. all truncate the data before writing. [b]a[/b] is for appending, but because you are editing certain lines you can't use it. Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/#findComment-56122 Share on other sites More sharing options...
tomfmason Posted July 11, 2006 Author Share Posted July 11, 2006 can you give me an example of writing this file into an array or string Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/#findComment-56123 Share on other sites More sharing options...
kenrbnsn Posted July 11, 2006 Share Posted July 11, 2006 Actually, you just need one modification to your code ... if the line doesn't match the one you're looking for, just write it out as is. Also move the fclose() to after the foreach loop.[code]<?phpforeach($file as $line_number => $content) { if (trim($content) == $layout) { fwrite($handle, $somecontent); } fwrite($handle,$content."\n");}fclose($handle);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/#findComment-56125 Share on other sites More sharing options...
tomfmason Posted July 11, 2006 Author Share Posted July 11, 2006 ken, I tired what you suggested and it just repeats mysite and mysite2Here is test_named.conf before[code=php:0]zone "mysite.com" IN { type master; file "mysite.com.zone";};zone "mysite2.com" IN { type master; file "mysite2.com.zone";};zone "xxx.xx.xx.in-addr.arpa" { type master; file "xx.xx.xxx.rev";};[/code]and after[code=php:0]zone "mysite.com" IN { type master; file "mysite.com.zone";};zone "mysite2.com" IN { type master; file "mysite2.com.zone";};zone "xxx.xx.xx.in-addr.arpa" { type master; file "xx.xx.xxx.rev";};zone "mysite.com" IN { type master; file "mysite.com.zone";};zone "mysite2.com" IN { type master; file "mysite2.com.zone";};zone "xxx.xx.xx.in-addr.arpa" { type master; file "xx.xx.xxx.rev";};[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/#findComment-56131 Share on other sites More sharing options...
tomfmason Posted July 11, 2006 Author Share Posted July 11, 2006 now I tried it this way [code=php:0]<?php$file = file('C:\\windows\\system32\\dns\\etc\\test_named.conf');$filename = 'C:\\windows\\system32\\dns\\etc\\test_named.conf';$lastsite = "mysite2.com";$lastzone = "mysite2.com.zone";$layout = "zone \"$lastsite\" IN { type master; file \"$lastzone\";};";$handle = fopen($filename, "a");$newdomain = "mysite3.com";$newzone = "mysite3.com.zone";$somecontent = "zone \"$newdomain\" IN { type master; file \"$newzone\";};"; foreach($file as $line_number => $content) { if (trim($content) == $layout) { fwrite($handle, $somecontent); } fwrite($handle,$somecontent."\n");}fclose($handle);?>[/code][b] The result[/b][code=php:0]zone "mysite.com" IN { type master; file "mysite.com.zone";};zone "mysite2.com" IN { type master; file "mysite2.com.zone";};zone "xxx.xx.xx.in-addr.arpa" { type master; file "xx.xx.xxx.rev";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};zone "mysite3.com" IN { type master; file "mysite3.com.zone";};[/code]as you cab see it just repeats it's self over and over again Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/#findComment-56137 Share on other sites More sharing options...
kenrbnsn Posted July 11, 2006 Share Posted July 11, 2006 Look at my suggestion again. The fwrite() when the if condition is not met is different than when the if condition is met. It will work.Ken Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/#findComment-56152 Share on other sites More sharing options...
tomfmason Posted July 11, 2006 Author Share Posted July 11, 2006 [b]The Fix[/b]I added a line in the test_named.php[code=php:0]#BEGIN AUTO RECORDSzone "mysite2.com" IN { type master; file "mysite2.com.zone";};zone "mysite.com" IN { type master; file "mysite.com.zone";};zone "xxx.xx.xx.in-addr.arpa" { type master; file "xx.xx.xxx.rev";};[/code]Then I edited the update.php[code=php:0]<?php$file = 'C:/windows/system32/dns/etc/test_named.conf'; $fp = fopen($file, 'r+'); $newdomain = "mysite3.com"; $newzone = "mysite3.com.zone"; $layout = " zone \"$newdomain\" IN { type master; file \"$newzone\"; }; "; while (!feof($fp)){ $line = trim(fgets($fp, 1024)); if ($line == '#BEGIN AUTO RECORDS'){ //Find where we are so we can come back. $fpos = ftell($fp); //First, read the rest of the file, so we don't overwrite it. $rest = fread($fp, filesize($file)); //Go back fseek($fp, $fpos, SEEK_SET); //Write the new data after the marker. fwrite($fp, "\n\n"); fwrite($fp, $layout); //Write back the rest of the data. fwrite($fp, $rest); } } fclose($fp);?>[/code]This will add the new domain like this[code=php:0]#BEGIN AUTO RECORDSzone "mysite3.com" IN { //the new domain type master; file "mysite3.com.zone";};zone "mysite2.com" IN { type master; file "mysite2.com.zone";};zone "mysite.com" IN { type master; file "mysite.com.zone";};zone "xxx.xx.xx.in-addr.arpa" { type master; file "xx.xx.xxx.rev";};[/code]The only issue that I see that maybe a problem is that there is a large space after the #BEGIN AUTO RECORDS . I have created a basic form to add a new domain(this is just a test) You can see it here[url=http://www.owpt.biz/home/test/open.php]http://www.owpt.biz/home/test/open.php[/url]Add a domain to see what I meanAny suggestions would be great. Quote Link to comment https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/#findComment-56282 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.