File function help needed

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!

Moderator: General Moderators

Post Reply
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

File function help needed

Post by pHp_n0ob »

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()
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: File function help needed

Post by Celauran »

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?
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: File function help needed

Post by pHp_n0ob »

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

Code: Select all

<?php
if(isset($_POST['msg']))
$msg=stripslashes($_POST['msg']);
$file="Lovemsg.txt";
fopen($file,"a+");
fwrite($file,$msg.'\n');
fclose($file);
$nick=$_GET["nick"];
$chatroom=$_GET["croom"];
echo '<form method="post" action=""><input type="text"
name="msg"/><input type="submit" name="Post" value="Send"/></form>
You are Logged In as '.$nick.' in chatroom '.$chatroom.'<br/>';
echo file_get_contents("Lovemsg.txt");
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: File function help needed

Post by califdon »

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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: File function help needed

Post by Celauran »

So read each line into an array and then reverse the array.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: File function help needed

Post by Christopher »

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.
(#10850)
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: File function help needed

Post by pHp_n0ob »

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
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: File function help needed

Post by pHp_n0ob »

Celauran wrote:So read each line into an array and then reverse the array.
you mean something like array_reverse


but how to read lines through array reverse
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: File function help needed

Post by Christopher »

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.
(#10850)
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: File function help needed

Post by pHp_n0ob »

Christopher wrote:
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) :D .....please also tell that how can I list those files (with text contents and sort newest at top)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: File function help needed

Post by Christopher »

pHp_n0ob wrote:I am going to try whatever you said..(seperate file for each text) :D
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.
(#10850)
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: File function help needed

Post by pHp_n0ob »

Christopher wrote:
pHp_n0ob wrote:I am going to try whatever you said..(seperate file for each text) :D
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
Post Reply