Jump to content
xisto Community
Sign in to follow this  
leiaah

How To Write At The Start Of A File without affecting its exisitng content

Recommended Posts

how do you write at the start of a file when it already has existing contents?...Like appending but not starting from the point where the last entry was made but from the top.like in blogs, i mean.

Share this post


Link to post
Share on other sites

the easiest way is to read the entire contents of the file into a variable.1. open the file for reading2. read contents into variable3. close file4. open file for writing (overwrites all data currently in the file)5. write new data to file6. write old data in the variable to the file7. close the file

Share this post


Link to post
Share on other sites

the easiest way is to read the entire contents of the file into a variable.

1. open the file for reading

2. read contents into variable

3. close file

4. open file for writing (overwrites all data currently in the file)

5. write new data to file

6. write old data in the variable to the file

7. close the file

42630[/snapback]


I'd prefer

 

1. open a tempfile, write new data to the tempfile

2. open the datafile for reading

3. read contents into the tempfile

4. close the datafile

5. close the tempfile

6. rename datafile,like this, you can get a backup to last writing

7. rename the tempfile to original datafile name

Share this post


Link to post
Share on other sites

like in blogs, i mean.

42558[/snapback]

Like in blogs you don't save each blog entry in one big ugly file. You save each entry either in separate files, or in separate records in a database.

 

If you want to do this for a blog, don't save everything to one file, seriously. If you have Xisto hosting there are lots of click and go blog systems you can install. But if you want to build something from scratch I'd store blog entries in a database and then for your front page do something like (this works with MySQL) "SELECT * FROM entries ORDER BY date DESC limit 0, 5". I think ordering descending will give you the newest first, but I'm not sure, if not, just replace DESC with ASC.

Share this post


Link to post
Share on other sites

hey the guy asked a question so he doesnt want alot of stuff, just give him the damn code, heres how you write at the begining of a file..

<?php//the file variable$filename='file.txt';//get contents of the file to the variable $ht$ht=implode('',file($filename));//the text you wish to write,note the \n to start a new line, dont forget it$txt="This is the text you wish to write at the beggining of a file\n;//open the file for writing, open in write mode, delete all previous content.$handle=@fopen($filename,'w');//join the previous contents of the file, with the new content$txt .= $ht;//finally write your text to the filefwrite($handle,$txt);//close the filefclose($handle);//to be on the safe side include some error handling?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.