Hi,
You have to open the file, write to the file, then close the file.
fopen("fileToOpen", "mode", "optional--SearchIncludePath");
I use the optional third parameter (1) so I don't have to put the full path of
the fileToOpen. The mode parameter is for how you want to open the file.
Modes
r = read from start of file
r+ = read and write from start of file
w = write--will delete contents in file if it exists
w+ = write and read--will delete contents in file if it exists
a = append--starts from end of existing contents
a+ = append, read, wirte--starts from end of existing contents
b = binary--use in conjunction with another mode
fwrite("fileToWriteTo", "dataToSave", optional--length);
The third parameter is for the maximum number of bytes to wirte to the file.
fclose($openFile);
Pretty simple, has a true or false value but I usually don't check if the file
has been closed.
This code worked fine for me:
<?php
$fileName = "staff.txt";
$openFile = fopen($fileName, "w", 1);
if(!$openFile)
{
echo "<p>The file, ".$fileName.", could not be opened.</p>";
}
$outputString = "This is the data that you want to add to the file.";
$writeToFile = fwrite($openFile , $outputString);
if(!$writeToFile)
{
echo "<p>Could not wirte to ".$fileName.".</p>";
}
fclose($openFile);
?>
Hope this helps.
Tanya Holliday
slimjimbuk <[EMAIL PROTECTED]> wrote:
Hi,
What's the easiest and safest way to use PHP to open a file, add a new
line and save the amended file? The line I want to add is not at the
top or the bottom of the file - it is to be added directly after the
first line in the file.
Thanks.
Jim
The php_mysql group is dedicated to learn more about the PHP/MySQL web database
possibilities through group learning.
---------------------------------
YAHOO! GROUPS LINKS
Visit your group "php_mysql" on the web.
To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
---------------------------------
[Non-text portions of this message have been removed]
------------------------ Yahoo! Groups Sponsor --------------------~-->
<font face=arial size=-1><a
href="http://us.ard.yahoo.com/SIG=12hbmm9sr/M=362329.6886308.7839368.1510227/D=groups/S=1705375618:TM/Y=YAHOO/EXP=1124352639/A=2894321/R=0/SIG=11dvsfulr/*http://youthnoise.com/page.php?page_id=1992
">Fair play? Video games influencing politics. Click and talk back!</a>.</font>
--------------------------------------------------------------------~->
The php_mysql group is dedicated to learn more about the PHP/MySQL web database
possibilities through group learning.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/php_mysql/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/