PDA

View Full Version : PHP write txt file-beginner question



techjosh
05-12-2009, 11:34 PM
Hi guys. My question is how do I use the
$file=fopen("welcome.txt","xx");
command with write commands such as (replacing xx with) w, w+, a, x, x+

I'm continuing my efforts to create php style databases and am trying to build from the basics. It's simple enough to use the command above for reading files, I want to know how to write files now with the same command. It seems to be overcomplicated way too much anywhere I go. I'm getting this stuff specifically from http://www.w3schools.com/php/php_file.asp.

Thx in advance for any help. I'll post my code if requested.

dataman
05-18-2009, 04:07 AM
I use the "w+" option for writing the output file. The use the "fputs" statement to write the output record.

DataMan

JamesYap
06-01-2009, 01:42 PM
http://my2.php.net/fputs

<?php

// file container where all texts are to be written
$fileContainer = date("MjY").'.log';

// open the said file
$filePointer = fopen($fileContainer,"w+");

// text to be written in the file
$logMsg = "You are located at ".$_SERVER["REMOTE_ADDR"]."\n";

// below is where the log message has been written to a file.
fputs($filePointer,$logMsg);

// close the open said file after writing the text
fclose($filePointer);

?>

techjosh
06-01-2009, 03:20 PM
Thank you both for your input. I did eventually figure it all out. I'm doing a bunch of php to manage data to and from the server via saved text files on the server. If anyone wants to know more I'll be willing to post some code. I'm just using simple code and no database applications such as mysql