PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm allowing my users to write in a .txt file through html forms.....but HOW can i display the .txt file in order to the newest post...
For eg. (1). A user "bdplanet" wrote "hi"
(2). Another user "soroze" wrote "how r u"
(3). Another user "vivek" wrote "how r u both"....
Then it should display the text file in order:-
Vivek >How r u both
Soroze >How r u
Bdplanet >hi
...just like a chat...i tried fwrite with every mode a,a+,r,r+ bt failed
also suggest file_get_contents is good or readfile()
Celauran wrote:You'll want a+ for fopen. Please post your code and explain where you're running into problems. Also, why are you writing this to a file?
1. a+ will put the cursor at end,so its showing newest post at last.
2. the problem is i want to save the whole data in a single flat file...with exception that IT MUST SHOW THE NEWEST POST AT TOP/FIRST
3. i am saving data because i am just trying to design a simple flat file chat script.
Here is the coding
Your approach is equivalent to using a screwdriver to drive nails into a board. It's possible, but it is most assuredly not the easy way to do the job. Use a hammer! In this case, you are trying to use a flat file as a database. Wrong tool! If this is going to be a small application, or just a demo, you could use SQLite, which does not require installing a database server, or better yet, use MySQL. I can think of no possible justification for using a text file for this kind of purpose.
This is a fairly poor idea, but if you really want to do it then I would recommend a few things:
- Separate reading and writing
- For writing, lock the file and append lines to the end of the file.
- For reading, pass a URL parameter (or post var) with the last position of the end of the file. Then you can output from that point to the end of the file to show what has been posted.
You will need to probably have a unique file per conversation. That will provide a transcript, but will need to be deleted.
If you design a clean interface and a layered architecture with your datasource abstracted, then you can refactor later to a database.
Christopher wrote:This is a fairly poor idea, but if you really want to do it then I would recommend a few things:
- Separate reading and writing
- For writing, lock the file and append lines to the end of the file.
- For reading, pass a URL parameter (or post var) with the last position of the end of the file. Then you can output from that point to the end of the file to show what has been posted.
You will need to probably have a unique file per conversation. That will provide a transcript, but will need to be deleted.
If you design a clean interface and a layered architecture with your datasource abstracted, then you can refactor later to a database.
Yes, I know that seperate files for each conversation can do it......but it looks ugly to use so much files and then deleting them
pHp_n0ob wrote:.but it looks ugly to use so much files and then deleting them
Looks ugly to whom? Lots of small files is what Unix finds beautiful. Each will have timestamps, so you can delete by date. And you can name them so they can be deleted by matching parts of the file names. All these capabilities are incredibly fast in modern filesystems. That's the only beautiful part to this ugly idea.
pHp_n0ob wrote:.but it looks ugly to use so much files and then deleting them
Looks ugly to whom? Lots of small files is what Unix finds beautiful. Each will have timestamps, so you can delete by date. And you can name them so they can be deleted by matching parts of the file names. All these capabilities are incredibly fast in modern filesystems. That's the only beautiful part to this ugly idea.
Ok...thanx for suggestion and explaination,I am going to try whatever you said..(seperate file for each text) .....please also tell that how can I list those files (with text contents and sort newest at top)
pHp_n0ob wrote:I am going to try whatever you said..(seperate file for each text)
I said one file per chat session.
pHp_n0ob wrote:.....please also tell that how can I list those files (with text contents and sort newest at top)
The glob() function is an easy way to show files. You may want put information in the file name to help find specific sets of files.
Can you please help me out with sessions...I am unable in handling them(I'm a n0ob now, thinking of being an xpert by hardwork ), thats the main reason that I am using $_GET[] throughout the whole script for nicknames.Also suggest a proper method to pass on sessions through links to different pages...thanks