Page 1 of 1
File function help needed
Posted: Wed Sep 26, 2012 7:09 am
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()
Re: File function help needed
Posted: Wed Sep 26, 2012 10:25 am
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?
Re: File function help needed
Posted: Wed Sep 26, 2012 1:23 pm
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");
?>
Re: File function help needed
Posted: Wed Sep 26, 2012 1:53 pm
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.
Re: File function help needed
Posted: Wed Sep 26, 2012 5:19 pm
by Celauran
So read each line into an array and then reverse the array.
Re: File function help needed
Posted: Wed Sep 26, 2012 6:59 pm
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.
Re: File function help needed
Posted: Wed Sep 26, 2012 9:15 pm
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
Re: File function help needed
Posted: Wed Sep 26, 2012 10:28 pm
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
Re: File function help needed
Posted: Thu Sep 27, 2012 1:13 pm
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.
Re: File function help needed
Posted: Thu Sep 27, 2012 10:58 pm
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)

.....please also tell that how can I list those files (with text contents and sort newest at top)
Re: File function help needed
Posted: Fri Sep 28, 2012 9:45 am
by Christopher
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.
Re: File function help needed
Posted: Fri Sep 28, 2012 12:29 pm
by pHp_n0ob
Christopher wrote: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