Jump to content

fwrite(), fopen(), fread(). Writing to a file, and reading it on a page


tyty1993

Recommended Posts

I need some help here. I am trying to write/edit a .txt file, then try to display it on another page. I then try to display the contents of the file in a text field value which then goes wrong. It works when I use "r+" but it adds data to the file. When  I use "w+" however, it doesn't display it in the text field when i call the fread() function. Here is some of the code that I am using:

 

PHP File 1: (to write data to file)

<?php
$file = $_POST['file'];
$data = $_POST['data'];
$handling = fopen($file, 'w+');
$read = fread($handling, 1024);

//Output Page
include('/home/www/mywebsite.com/edit_body.html');
?>

 

HTML Code 1 (select a file to edit, and hit submit to write/edit data)

<form method="post" action="edit.php">
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr>
<td align="left">
File to Edit
</td>
<td align="left">
<select name="file" id="file">
<option value="file1.txt">File 1</option>
<option value="file2.txt">File 2</option>
<option value="file3.txt">File 3</option>
</select>
</td>
</tr>
<tr>
<td align="left">
<input type="submit" value="Submit" name="submit" id="submit">
</td>
</tr>
</table>
</form>

 

HTML Code 2 (edit.php/edit_body.html)(writes data to read)

<form method="post" action="<?php echo $PHP_SELF; ?>">
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr>
<td align="left">
File You're Editing:
</td>
<td align="left">
<select name="file" id="file">
<option value="<?php echo $file; ?>"><?php echo $file; ?></option>
</select>
</td>
</tr>
<tr>
<td align="left">
Data
</td>
<td align="left">
<input type="text" name="data" id="data" value="<?php echo $read; ?>" size="15">
</td>
</tr>
<tr>
<td align="left">
<input type="submit" value="Submit" name="submit" id="submit">
</td>
</tr>
</table>
</form>
<?php
fwrite($handling, $data);
fclose($handling);
?>

 

PHP File 2 (for read_body.html)

<?php
//File 1
$1 = "file1.txt";
$handle1 = fopen($1, 'r+');
$file1= fread($handle1, 1024);

//File 2
$2 = "file2.txt";
$handle2 = fopen($2, 'r+');
$file2 = fread($handle2, 1024);

//File 3
$3 = "file3.txt";
$handle3 = fopen($3, 'r+');
$file3 = fread($handle3, 1024);

//Output Page
include('/home/www/mywebsite.com/read_body.html');
?>

 

HTML Code 3 (reads data written, read_body.html)


<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr>
<td align="center">
<b>File</b>
</td>
<td align="center">
<b>Content</b>
</td>
</tr>
<tr>
<td align="center">
<?php echo $1; ?>
</td>
<td align="center">
<?php
fclose($handle1);
echo $file1;
?>

</td>
</tr>
<tr>
<td align="center">
<?php echo $2; ?>
</td>
<td align="center">
<?php
fclose($handle2);
echo $file2;
?>

</td>
</tr>
<tr>
<td align="center">
<?php echo $3; ?>
</td>
<td align="center">
<?php
fclose($handle3);
echo $file3;
?>

</td>
</tr>
</table>

 

Yeah, I know it's very complicated, but i need someone's help. Most of the HTML files are included within the php file. Can anyone help me?

Well, the fread() is used to read the data that was written, as somewhat shown here...

 

HTML Code 3 (reads data written, read_body.html)


<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr>
<td align="center">
<b>File</b>
</td>
<td align="center">
<b>Content</b>
</td>
</tr>
<tr>
<td align="center">
<?php echo $1; ?>
</td>
<td align="center">
<?php
fclose($handle1);
echo $file1;
?>

</td>
</tr>
<tr>
<td align="center">
<?php echo $2; ?>
</td>
<td align="center">
<?php
fclose($handle2);
echo $file2;
?>

</td>
</tr>
<tr>
<td align="center">
<?php echo $3; ?>
</td>
<td align="center">
<?php
fclose($handle3);
echo $file3;
?>

</td>
</tr>
</table>

 

So, I use one page (PHP Code 1/HTML Code 1) to write to/edit a text file, and read what the data was written from the text file that was just written. (PHP Code 2/HTML Code 3). doesn't w+ means writing and reading?

doesn't w+ means writing and reading?

 

correct.

 

// Open file stream
$fileStream = fopen('myFile.txt', 'w+');

// Write to our file
fwrite($fileStream, "Hello World!");

// Read the file
$fileContainer = fread($fileStream, filesize('myFile.txt'));

// Echo the content of myFile.txt
echo $fileContainer;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.