Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 11-10-2006, 08:33 PM   #1 (permalink)
 
Newb Techie

Join Date: Oct 2006

Posts: 29

zach014

Default 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
zach014 is offline  
Old 11-12-2006, 04:25 PM   #2 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,421

office politics will become famous soon enough

Default

copy the file contents to a variable, append the variable to the new string, write the newest string.
office politics is offline  
Old 11-15-2006, 12:32 PM   #3 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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");
__________________
Vormund is offline  
Old 11-16-2006, 09:09 PM   #4 (permalink)
 
Newb Techie

Join Date: Oct 2006

Posts: 29

zach014

Default

thanks csamuels, but can i make it so it simply adds onto the file instead of overwriting into the file?
zach014 is offline  
Old 11-20-2006, 02:56 PM   #5 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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).
__________________
Vormund is offline  
Old 11-20-2006, 07:01 PM   #6 (permalink)
 
Newb Techie

Join Date: Oct 2006

Posts: 29

zach014

Default

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
zach014 is offline  
Old 11-20-2006, 08:22 PM   #7 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,421

office politics will become famous soon enough

Default

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($handlefilesize($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
office politics is offline  
Old 11-20-2006, 08:32 PM   #8 (permalink)
 
Newb Techie

Join Date: Oct 2006

Posts: 29

zach014

Default

Just a question; how to delete and how to make a new file.
zach014 is offline  
Old 11-23-2006, 12:40 PM   #9 (permalink)
 
Newb Techie

Join Date: Oct 2006

Posts: 29

zach014

Default

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($handlefilesize($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 is offline  
Old 11-27-2006, 06:23 PM   #10 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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);
?>

Vormund is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On