Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with fopen() command

Status
Not open for further replies.

crazybeans128

Programmer
Aug 5, 2003
79
US
Okay, here is the syntax i am using.

$fp=fopen($gbfile,'r+');
fwrite($fp, "Yo\n*****\nDude");
fclose($fp);

Everytime time I try to write to the file, it will simply overwrite what was there, if i have more than that, it won't write it. From what I've been reading, it should add that to the top of the file, but it just erases the file and starts over. What is wrong?

 
Here's what the manual at php.net says about the modes when opening a file:
Code:
[b]mode	Description[/b]
'[COLOR=red]r[/color]'	Open for reading only; place the file pointer at the beginning of the file.
'[COLOR=red]r+[/color]'	Open for reading and writing; place the file pointer at the beginning of the file.
'[COLOR=red]w[/color]'	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.
'[COLOR=red]w+[/color]'	Open for reading and writing; 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.
'[COLOR=red]a[/color]'	Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'[COLOR=red]a+[/color]'	Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
I believe you want to use 'a+' not 'r+' if you want to append to the file.

Ken
 
Well I want to write to the beginning of the file. That's why I am using r+. Something just keeps going wrong, it overwrites everything as though i were using w+, but I'm not.

 
Also, I forgot to mention that using the other commands work. I can appen the file, or overwrite the whole thing with a+. I guess it mainly comes down to I am trying to read the file from the newest line to the oldest. I haven't figured that out yet.

 
You are misunderstanding what r+ means.
Your concept is that it prepends the existing file content. That is not the case (as you are actually experiencing).
In order to prepend something to a file you need to read the original content into a string, then prepend the new addition, and recommit the concatenated string to the file.

There is no writing of data to the beginning of the file other than the described procedure. r+ resets the file to the beginning and will write whatever you commit over what exists.

What you want to do is:
read the file using file(). It will end up in an associative array. Go to the end of the array and iterate it backwards. That's how you get a the newest lines first.
 
Well I am a beginner in PHP, so how would I go backwards through it?

 
Use a loop that decrements
Code:
# read the file
$theLines = file('/fs/wahtever/file.txt');
# how many lines?
$numLines = count($theLines);
for ($i = $numLines; $i>0; $i--){
    echo $theLines[$i-1]."<br>";
}
ANother way would be to first reverse the array and the increment:
Code:
# read the file
$theLines = file('/fs/wahtever/file.txt');
# reverse
$rLines = array_reverse($theLines);
# foreach loop
foreach ($rLines AS $line){
   echo $line."<br>";
}
These are the basic concepts.
 
Sounds good. As soon as my web server is up, I will test that out.

 
Success! It works, and i now have a script that opens the file, reads the inner text, and i am currently experimenting with script adding the new posts. with your guys help, it is the shortest script i have seen, and i looked up a bunch of stuff. but i have a feeling there is still more to come.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top