<?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);
?>
$new = "this is my new string";
$new = $new.$contents;
<?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
<?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);
?>
<?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);
?>