Forum Moderators: coopster

Message Too Old, No Replies

fopen() function

littl help?

         

tonynoriega

8:35 pm on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



trying to learn the fopen() command for a php file i have. Below is a snippet of a display on the page....it will be used for my agents to type weekly notes....so i want it to read a file, that will populate this text area, and then each week they can re type the notes, and resubmit the file, and have the new notes appear....

from what i have been reading, i need to use:

fopen(filename,mode,include_path,context)

But do i need fclose also?
Do i need to create an original text file?
Will this file be able to be read into the <textarea> or should i just throw it into the <td> file here </td>

just looking for some insight, or a realworld example.

thanks

$display .= '<tr colspan="2" height="5px"><td width="25%" align="center" bgcolor="#DADCBE"><strong>Summary</strong></td>
<td align="center" bgcolor="#DADCBE"><strong>Weekly Notes</strong></td></tr>
<tr><td valign="bottom">Return Visitors: '. $reg_last_visit . '</td>
<td rowspan="2">
<form name="form1" method="post" action="">
<textarea name="textarea" cols="87" rows="10"></textarea>
</form>
</td></tr>
<tr><td valign="bottom">Total Weekly Traffic: '. $tc . '</td></tr>';

SteveLetwin

10:26 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



Well here's an insane example.


<?php
$handle = fopen("/etc/passwd", "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
<html><body><form>
<textarea>
<?php echo $contents?>
</textarea>
</form></body></html>

The include path and context are optional for the fopen. And one should always close any files one opens.

[edited by: SteveLetwin at 10:27 pm (utc) on Aug. 21, 2007]

SteveLetwin

6:49 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



For pedantry's sake, if you use this, make sure to change the above code to either

1)


$handle = fopen("/etc/passwd", "r");
$contents = fread($handle, filesize("/etc/passwd"));

or 2)


$filename = "/etc/passwd";
$handle = fopen($filename, "r");

of course you should also probably be reading and displaying a different file....