Jump to content

Archived

This topic is now archived and is closed to further replies.

tomfmason

fwrite() questions

Recommended Posts

I am trying to create a script that will create a zone file in my dns directory (etc) and update my httpd.conf when I buy a new domain or add a subdomain.

So far I am able to create the zone file just fine but I am unable to write anything to the file. I am using an example straight from the manual on [url=http://www.php.net/manual/en/function.fwrite.php]fwrite()[/url]. So far I get the following message

[code]The file C:\windows\system32\dns\mysite.com.zone is not writable[/code]

here is my fopen() file that creates the zone file

[code=php:0]<?php
$filename ="mysite.com.zone";
fopen("C:\\windows\\system32\\dns\\etc\\$filename", "x+");
?>[/code]

And here is the fwrite() file

[code=php:0]<?php
$filename = 'C:\\windows\\system32\\dns\\mysite.com.zone';
$somecontent = "
$TTL 900 ; 900 seconds default record (T)ime (T)o (L)ive in cache

@ 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\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

  // In our example we're opening $filename in append mode.
  // The file pointer is at the bottom of the file hence
  // that's where $somecontent will go when we fwrite() it.
  if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
  }

  // Write $somecontent to our opened file.
  if (fwrite($handle, $somecontent) === FALSE) {
      echo "Cannot write to file ($filename)";
      exit;
  }
 
  echo "Success, wrote ($somecontent) to file ($filename)";
 
  fclose($handle);

} else {
  echo "The file $filename is not writable";
}
?>[/code]


I am also wondering how I would write or edit a specific line on a file?
Any suggestions would be great.

Share this post


Link to post
Share on other sites
I fixed the fwrite()

[b]The Fix[/b]

[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]

My qestion about write to a specific line is still unanswered. Also how would I reload bind via php?

Share this post


Link to post
Share on other sites
i've tried to modify the file dso that it also writes some more data, like this here - three digits: 257 (the choice of signs=>digits is random)
well, it does write it, but the number 257 is written into the comments.txt file three times: like this:
257text
257
257

what to do to make it write the data only once?

<?php


$filename = 'comments.txt';

$handle = fopen($filename, "a");

$somecontent = "$comment\n257";

fwrite($handle, $somecontent);

fclose($handle);

?>

Share this post


Link to post
Share on other sites
oh - would have forgotten this script is in one file with another script, which provides the value of $comment ...
maybe this changes the thinking to it....

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.