Re: Writing to a text file
| From: | jeremy brand | Date: | Sun, 10 Sep 2000 23:50:39 +0000 |
| Subject: | Re: Writing to a text file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-16094@lists.php.net to get a copy of this message | ||
You only need to open the file once. Do it before your loop. Use
fwrite inside your loop, then after you are done with the loop, use
fclose.
The problem is that every time you fopen with "w" is sets the file
size to zero.
-jeremy brand
_______________________________________________________________________
GnuPG Fingerprint: 9F5E 95E0 1CC2 E054 84EA 6099 8ACD 9DBA D2C0 5EA9
_ http://www.JeremyBrand.com/Jeremy/Brand/Jeremy_Brand.html
for more __
On Sun, 10 Sep 2000 php@hotchilli.com wrote:
> Date: Sun, 10 Sep 2000 23:22:56 +0100
> From: php@hotchilli.com
> To: php-general@lists.php.net
> Subject: [PHP] Writing to a text file
>
> Hi
>
> I an querying MySQL and trying to write the results to a text file called
> "httpd.conf"
>
> All it writes is:
>
> <VirtualHost 213.165.144.13>
> Options ExecCGI Includes
> DocumentRoot /home/site00042/public_html
> ServerAdmin webmaster@another.com
> ServerName www.another.com
> ServerAlias
> CustomLog /home/logs/site00042/access_log combined
> ScriptAlias /cgi-bin/ /home/site00042/cgi-bin/
> </VirtualHost>
>
> It should write 42 of these blocks - this just happens to be the last one.
>
> Here is my code - what basic mistake am I making?
>
> <?php
>
> $user= "root";
> $pass= "";
> $host= "localhost";
> $db= "serveradmin";
> $link = mysql_connect ("localhost", "$user", "$pass")
> or die ("Could not connect");
>
> mysql_select_db($db,$link)
> or die ( "Could not Open $db: ".mysql_error() );
>
> $result = mysql_query( "Select site_id, host_name, domain_name, ip_address FROM
> sites" );
>
> while ( $row = mysql_fetch_row($result ))
> {
> $site_id = $row[0];
> $host_name = $row[1];
> $domain_name = $row[2];
> $ip_address = $row[3];
>
>
> $filename = "httpd.conf";
> $fp = fopen( "httpd.conf", 'w' );
> print "Writing to $filename $row[1].$row[2]<br>";
> $fp = fopen( $filename, "w" ) or die("Couldn't write to $filename");
>
> flock( $fp, 2); //exclusive lock
> fputs( $fp, "
> <VirtualHost $row[3]>
> Options ExecCGI Includes
> DocumentRoot /home/site$row[0]/public_html
> ServerAdmin webmaster@$row[2]
> ServerName $row[1].$row[2]
> ServerAlias
> CustomLog /home/logs/site$row[0]/access_log combined
> ScriptAlias /cgi-bin/ /home/site$row[0]/cgi-bin/
> </VirtualHost>\n" );
> flock( $fp, 1 ); // release the lock
> fclose( $fp );
> }
> ?>
>
>
> Thanks
>
> Tony
> PS This is what it prints on the screen - just does not write all thse to the file.
>
>
>
>
> Writing to httpd.conf www.hotchilli.com
> Writing to httpd.conf www.hotchilli.co.uk
> Writing to httpd.conf www.csltech.co.uk
> Writing to httpd.conf www.pitman.co.uk
> Writing to httpd.conf www.thehouseonthe.net
> Writing to httpd.conf www.chillicart.com
> Writing to httpd.conf www.chillimonster.com
> Writing to httpd.conf www.fred.com
> Writing to httpd.conf www.john.com
> Writing to httpd.conf www.hotchilli.org
> Writing to httpd.conf secure.processing.co.uk
> Writing to httpd.conf www.tonyturner.com
> Writing to httpd.conf www.happy.co.uk
> Writing to httpd.conf secure.processing.co.uk
> Writing to httpd.conf secure.processing.co.uk
> Writing to httpd.conf secure.processing.co.uk
> Writing to httpd.conf www.claire.com
> Writing to httpd.conf www.andrew.co.uk
> Writing to httpd.conf www.robert.com
> Writing to httpd.conf www.chillimonster.co.uk
> Writing to httpd.conf www.chillicart.uk.com
> Writing to httpd.conf secure.pitman.com
> Writing to httpd.conf www.johnson.com
> Writing to httpd.conf www.johnson.co.uk
> Writing to httpd.conf www.fdd.co.uk
> Writing to httpd.conf www.boyanet.co.uk
> Writing to httpd.conf www.hargroves.co.uk
> Writing to httpd.conf www.newsite.com
> Writing to httpd.conf www.oldsite.com
> Writing to httpd.conf www.microsoft.com
> Writing to httpd.conf www.softsource.com
> Writing to httpd.conf www.softsource.co.uk
> Writing to httpd.conf www.james.com
> Writing to httpd.conf www.james.co.uk
> Writing to httpd.conf www.happy.com
> Writing to httpd.conf www.happy.co.uk
> Writing to httpd.conf www.csltech.com
> Writing to httpd.conf www.tes.com
> Writing to httpd.conf www.test1.co.uk
> Writing to httpd.conf www.test3.com
> Writing to httpd.conf www.fred.com
> Writing to httpd.conf www.another.com
>
>