Jump to content


Photo

fwrite() questions


  • Please log in to reply
3 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 01:29 AM

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 fwrite(). So far I get the following message

The file C:\windows\system32\dns\mysite.com.zone is not writable

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

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

And here is the fwrite() file

<?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";
}
?>


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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 02:17 AM

I fixed the fwrite()

The Fix

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

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 rysio

rysio
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 January 2007 - 10:29 PM

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

?>

#4 rysio

rysio
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 January 2007 - 11:38 PM

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....




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users