Jump to content

[SOLVED] fread()


Brian W

Recommended Posts

I have a form that Posts to a .php with the code below. I want it to take the original content of the file and then add what I posted ontop. Right now it replaces the original text.

<?php
$handle = fopen('Games/Data1.html', 'w+');
$Read = fread('Games/Data1.html');
$Text = $_POST['input']."<br/>".$Read; ?>
<?php fwrite($handle, $Text) ?>
<?php fclose('Games/Data1.html');
header('location:Table.php')?>

I'm probably using the fread incorrectly. I am using PHP 4 I believe.

BTW, if you didn't gather, this kinda works like a chat file because it kinda is. lol    but not exactly.

Link to comment
Share on other sites

First off, i've made three changes to the code so  far. 2 of which I modified on the above post. the latest if fixed me dumb a** mistake that should have never even happened.

$Read = fread($handle);//thats a little better, but for all I know may still be incorrect

Still not adding the string on top, just rewriting the whole file with what i post.

Link to comment
Share on other sites

OH, thank you... I read "truncate to 0" and thought, "that doesn't good". So, I looked into it. Bad. redesigned code=

<?php
$filename = 'Games/Data1.html';
$handle = fopen($filename, 'x');
fclose($handle);
$handle = fopen($filename, 'r+');
$contents = fread($handle,"20000");
$Text = $_POST['input']."<br/>"; ?>
<?php fwrite($handle, $Text); ?>
<?php fclose($handle);
header('location:Table.php');?>

It is now not saving over, which is nice. I have the x one there to create the file if it doesn't exist. Now, how am I supposed to point the write to the beginning?

Link to comment
Share on other sites

The 'x' modeshould place the pointer at the beginning of the file.. so any fwrites should automatically go above... no need to fread().

 

<?php
$filename = 'Games/Data1.html';
$handle = fopen($filename, 'x');
$Text = $_POST['input']."<br/>"; ?>
fwrite($handle, $Text);
fclose($handle);
header('location:Table.php');?>

Link to comment
Share on other sites

I don't need to read, least I don't think I do.

NEW PROBLEM OCCURRED

Just as it started working. Suddenly it bugged out and stopped. WTF, really.

It was odd because it was working and then I opened 2 windows so I could chat with myself and suddenly it glitched and won't work anymore.

What it does now is replace what I have written, like when you hit the insert key (oh how I hate that key).

Link to comment
Share on other sites

rewind() my friend.

I figured out the problem and got it working, or at least it works. Like I said, it was writting over anything over what was already there. So, I had it read, then write my new stuff with what it read before attached to the end of it, essentially adding content every time. Topic solved, lesson learned.

Link to comment
Share on other sites

This is the ugly piece of crap I made. It works

<?php
$filename = 'Games/Data1.html';
$handle = fopen($filename, 'x');
fclose($handle);
$handle = fopen($filename, 'r+');
$content = fread($handle, filesize($filename));
rewind($handle);
$Text = $_POST['input']."<br>".$content; ?>
<?php fwrite($handle, $Text); ?>
<?php fclose($handle);
header('location:Table.php');?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.