Jump to content

fwrite() questions


tomfmason

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.
Link to comment
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?
Link to comment
Share on other sites

  • 6 months later...
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);

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.