|  |
11-10-2006, 08:33 PM
|
#1 (permalink)
|
Newb Techie Join Date: Oct 2006 Posts: 29
| PHP fwrite to beginning 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 |
| |
11-12-2006, 04:25 PM
|
#2 (permalink)
|
It's all just 1s and 0s Join Date: Jan 2004 Location: in the lab Posts: 4,421
| copy the file contents to a variable, append the variable to the new string, write the newest string. |
| |
11-15-2006, 12:32 PM
|
#3 (permalink)
|
Monster Techie Join Date: May 2004 Location: Tucson, AZ, USA Posts: 1,183
| 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");
__________________ |
| |
11-16-2006, 09:09 PM
|
#4 (permalink)
|
Newb Techie Join Date: Oct 2006 Posts: 29
| thanks csamuels, but can i make it so it simply adds onto the file instead of overwriting into the file? |
| |
11-20-2006, 02:56 PM
|
#5 (permalink)
|
Monster Techie Join Date: May 2004 Location: Tucson, AZ, USA Posts: 1,183
| 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).
__________________ |
| |
11-20-2006, 07:01 PM
|
#6 (permalink)
|
Newb Techie Join Date: Oct 2006 Posts: 29
| 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 |
| |
11-20-2006, 08:22 PM
|
#7 (permalink)
|
It's all just 1s and 0s Join Date: Jan 2004 Location: in the lab Posts: 4,421
| 1. get files current contents PHP Code: <?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 Code: $new = "this is my new string";
$new = $new.$contents;
4. write new file containing $new string |
| |
11-20-2006, 08:32 PM
|
#8 (permalink)
|
Newb Techie Join Date: Oct 2006 Posts: 29
| Just a question; how to delete and how to make a new file. |
| |
11-23-2006, 12:40 PM
|
#9 (permalink)
|
Newb Techie Join Date: Oct 2006 Posts: 29
| i'm guessing unlink is used, but somehow, it gives me errors
here's my code: PHP Code: <?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);
?> |
| |
11-27-2006, 06:23 PM
|
#10 (permalink)
|
Monster Techie Join Date: May 2004 Location: Tucson, AZ, USA Posts: 1,183
| Quote: Originally posted by zach014 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 Code: <?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 Code: <?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);
?> |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |