PHP fwrite to beginning

Status
Not open for further replies.

zach014

Baseband Member
Messages
29
how can i use fwrite to write a string to the beginning on a file, instead of the end, as default.

for example, say i wanted to make a forum-type thing. I would want to make each post "appear" at the beginning, not the end.

zach
 
Or you can fopen with 'r+', which is read/write; it places the pointer at the start of the file.

$h = fopen("file.txt", "r+");
fwrite($h, "I'm on the first line!\n");
 
thanks csamuels, but can i make it so it simply adds onto the file instead of overwriting into the file?
 
http://us2.php.net/fopen
If you're opening the file, just choose the appropriate mode. As I said, r+ will place the pointer at the beginning of the file, and it will not overwrite the previous.

Likewise, if you're using file_get_contents(), you would append the data, and then overwrite the old file (since all data you want is in that string).
 
sadly, that's not true. It does write into the file, replacing char for char as it goes along.

here's my page
test17.4mak.net/new

click post
 
1. get files current contents

PHP:
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>

2. destroy file

3. concatenate new data with old data

PHP:
$new = "this is my new string";
$new = $new.$contents;

4. write new file containing $new string
 
i'm guessing unlink is used, but somehow, it gives me errors

here's my code:

PHP:
<?php
$filename='index.htm';
// get contents of a file into a string
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

if(unlink('index.htm'))
{ 
return true; 
}else{ 
return false; 
}  

$name = $_POST['name'];
$text = $_POST['text'];
$content = "\n<font size=4>[b]".$name."[/b]</font>
".$text."
---
";
$content = $content.$contents;

$handlet = fopen('index.htm', 'w');
fclose($handlet);

?>
 
zach014 said:
sadly, that's not true. It does write into the file, replacing char for char as it goes along.

here's my page
test17.4mak.net/new

click post

You're using the correct mode?

Anyhow, just a little shortcut, instead of fopen / etc to get the file into a string, you can use:
$contents = file_get_contents("test.txt");

And yes, as you said,
Unlike("test.txt");
should remove that file, although not needed with the right mode (dare I say).

A sample being:

PHP:
<?php
$filename = "test.txt";
$contents = file_get_contents($filename);
$name = $_POST['name'];
$text = $_POST['text'];
// Note: I'm thinking you want this at the top of the file, so \n should be at the end.
$content = "<font size=4>[b]".$name."[/b]</font>
".$text."
---
\n";
$contents = $content . $contents;
// This will take care of "removing" the file for the new contents.
// w - Open for writing only; place the file pointer at the beginning of the file and truncate
// the file to zero length. If the file does not exist, attempt to create it.
$h = fopen($filename, 'w');
fwrite($h, $contents);
fclose($h);
?>

Or just appending the file:
PHP:
<?php
$filename= "test.txt";
$name = $_POST['name'];
$text = $_POST['text'];
// Note that the \n is not used here.
$content = "<font size=4>[b]".$name."[/b]</font>
".$text."
---
";
if (file_exists($filename))
{ // The file exists, so put the pointer at the beginning and add the content.
  $h = fopen($filename, 'r+');
  fwrite($h, $content."\n");
}
else
{ // The file does not exist, so create it...and add the contents without the newline character.
  $h = fopen($filename, 'w');
  fwrite($h, $content);
}
// Close our handle.
fclose($h);
?>
 
Status
Not open for further replies.
Back
Top Bottom