/    Sign up×
Community /Pin to ProfileBookmark

I am using a function of php called rewind(points to a file that has been opened by fopen()) and I have the following code.

[code=php]
$newsfile = ‘shownews.php’;
$handle = @fopen($newsfile, ‘a+’) or die(‘I was unable to open ‘ . $newsfile);
rewind($handle);
[/code]

That section of the code should work I think, it sets the news file,(this is for a news system btw? ), opens it using fopen using handle, and then the rewind is called. Now forgive me if I am wrong but rewind places the pointer at the begining of the file, so that if you were to write to the file, it would write it in at the begining? That is what I am wanting to do so that my news shows up at the top so people dont have to scroll down to see the latest news. Now this is my writing code.

[code=php]if((!empty($_POST[‘title’])) and (false === strpos($newslog, $_POST[‘title’])))
{

# Add the new news to the news file
fwrite($handle, “<b><h4><center>”.$title.”</center></b></h4><br>”);
fwrite($handle, “<b>Written by:</b> <i>”.$author.”</i><br>”);
fwrite($handle, $news.”<br>”);
fwrite($handle, “<hr>”);

# Update $newslog for use in output
$newslog .= “<b><h4><center>”.$title.”</center></b></h4><br>”;
$newslog .= “<b>Written by:</b> <i>”.$author.”</i><br>”;
$newslog .= “<br><br> “.$news.”<br>”;
$newslog .= “<hr>”;

}[/code]

I just dont see why the rewind isn’t working. Can anyone help me out?

Thanks,
[b]~[/b][i]Travis[/i]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ScleppelMay 05.2006 — From the manual:
Note: If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position.[/quote]

You probably need to read the file into a variable first, then overwrite your old file with the new stuff at the top and then the old data from the variable.
[code=php]$new = 'at the top';
$old = file_get_contents('somefile.txt');

$handle = fopen('somefile.txt', "w");

// append old content
fwrite($handle, $new . $old);

fclose($handle);[/code]
Copy linkTweet thisAlerts:
@Heavy_MetalauthorMay 06.2006 — Ok, Thanks, I got it set up again, and actually with a smaller code than before, and it is much more efficieant than before thanks to some changes I made while editing to what you suggested. Thanks Alot Man,

[b]~[/b][i]Travis[/i]
×

Success!

Help @Heavy_Metal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...